diff --git a/qsort.h b/qsort.h index 46edbd5..dc32535 100644 --- a/qsort.h +++ b/qsort.h @@ -102,14 +102,15 @@ static inline pret3 partition3(uint32_t array[], int low, int high, uint32_t piv /* index until smaller or eq elements lay */ int i = (low - 1); - /* index until smaller or eq elements lay */ - int i2 = (high + 1); + uint32_t pc = 0; /* traverse each element of the array */ /* compare them with the pivot */ - int j2 = high; // j2 > i; --j2) { #pragma GCC unroll 4 for (int j = low; j <= high; ++j) { + /* Branchless pivot-count */ + pc += (array[j] == pivot); + if(array[j] < pivot) { /* if element smaller than pivot is found */ /* swap it with the greater element pointed by i */ @@ -118,16 +119,28 @@ static inline pret3 partition3(uint32_t array[], int low, int high, uint32_t piv /* swap element at i with element at j */ swapit(&array[i], &array[j]); } - if(j2 > i) { - if (array[j2] > pivot) { - /* if element smaller than pivot is found */ - /* swap it with the greater element pointed by i */ - --i2; + } - /* swap element at i with element at j */ - swapit(&array[i2], &array[j2]); - } - --j2; + /* Can spare out the second loop in these cases */ + if(pc < 2) { + pret3 ret; + ret.leftend = i; + ret.rightend = i + 1; + return ret; + } + + /* index until smaller or eq elements lay */ + int i2 = (high + 1); + + #pragma GCC unroll 4 + for (int j = high; j > i; --j) { + if (array[j] > pivot) { + /* if element smaller than pivot is found */ + /* swap it with the greater element pointed by i */ + --i2; + + /* swap element at i with element at j */ + swapit(&array[i2], &array[j]); } }