optimization that does not work, but is maybe not totally bad
This commit is contained in:
parent
bc500c84e8
commit
cebc516125
37
qsort.h
37
qsort.h
@ -102,15 +102,14 @@ static inline pret3 partition3(uint32_t array[], int low, int high, uint32_t piv
|
|||||||
|
|
||||||
/* index until smaller or eq elements lay */
|
/* index until smaller or eq elements lay */
|
||||||
int i = (low - 1);
|
int i = (low - 1);
|
||||||
uint32_t pc = 0;
|
/* index until smaller or eq elements lay */
|
||||||
|
int i2 = (high + 1);
|
||||||
|
|
||||||
/* traverse each element of the array */
|
/* traverse each element of the array */
|
||||||
/* compare them with the pivot */
|
/* compare them with the pivot */
|
||||||
|
int j2 = high; // j2 > i; --j2) {
|
||||||
#pragma GCC unroll 4
|
#pragma GCC unroll 4
|
||||||
for (int j = low; j <= high; ++j) {
|
for (int j = low; j <= high; ++j) {
|
||||||
/* Branchless pivot-count */
|
|
||||||
pc += (array[j] == pivot);
|
|
||||||
|
|
||||||
if(array[j] < pivot) {
|
if(array[j] < pivot) {
|
||||||
/* if element smaller than pivot is found */
|
/* if element smaller than pivot is found */
|
||||||
/* swap it with the greater element pointed by i */
|
/* swap it with the greater element pointed by i */
|
||||||
@ -119,28 +118,16 @@ static inline pret3 partition3(uint32_t array[], int low, int high, uint32_t piv
|
|||||||
/* swap element at i with element at j */
|
/* swap element at i with element at j */
|
||||||
swapit(&array[i], &array[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;
|
||||||
|
|
||||||
/* Can spare out the second loop in these cases */
|
/* swap element at i with element at j */
|
||||||
if(pc < 2) {
|
swapit(&array[i2], &array[j2]);
|
||||||
pret3 ret;
|
}
|
||||||
ret.leftend = i;
|
--j2;
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user