fir

2024-03-27 11:04:02 UTC

the chellenge is simplify such quicksort code as far as you can

conditions

1) it should work proper way

2) should be not slower or only slightly slower

(so thsi is not optimistation for speed its more for being simple but

staing reasonably fast)

3) by simplify i mean maybe removing soem if or some other

element but not necassary rewriting names for shorter etc as names are

not much meaningfull here (so by simplifying i mainy understand

removing something or some recomposition for something better/clearer)

4) the basic quicksort has two calls inside this one is

opitmised to have only one , i think both versions are okay

but maybe thsi one with one call inside is maybe better

void QuicksortInts7(int* table, int lo, int hi)

{

int i=lo; int j=hi;

while (i<hi)

{

int pivot =table[(lo+hi)/2];

while (i<=j) // Partition

{

while (table[i]<pivot) i++;

while (table[j]>pivot) j--;

if (i<=j)

{

int t=table[i];table[i]=table[j];table[j]=t;

i++; j--;

}

}

if (lo < j){ QuicksortInts7(table, lo, j);} //recursion

lo=i; j=hi;

}

}

is there a way to simplify this?

conditions

1) it should work proper way

2) should be not slower or only slightly slower

(so thsi is not optimistation for speed its more for being simple but

staing reasonably fast)

3) by simplify i mean maybe removing soem if or some other

element but not necassary rewriting names for shorter etc as names are

not much meaningfull here (so by simplifying i mainy understand

removing something or some recomposition for something better/clearer)

4) the basic quicksort has two calls inside this one is

opitmised to have only one , i think both versions are okay

but maybe thsi one with one call inside is maybe better

void QuicksortInts7(int* table, int lo, int hi)

{

int i=lo; int j=hi;

while (i<hi)

{

int pivot =table[(lo+hi)/2];

while (i<=j) // Partition

{

while (table[i]<pivot) i++;

while (table[j]>pivot) j--;

if (i<=j)

{

int t=table[i];table[i]=table[j];table[j]=t;

i++; j--;

}

}

if (lo < j){ QuicksortInts7(table, lo, j);} //recursion

lo=i; j=hi;

}

}

is there a way to simplify this?