Witam! Ostatnio sprawdzałem w internecie czy jest jakiś algorytm sortowania, który jest lepszy od quicksort. Znalazłem Radix sort , który wydaje się być świetny , ale mam tutaj kod który właśnie używa tego algorytmu , tylko że ten kod sortuje liczby w tablicy rosnąco , a ja chciałbym żeby sortował malejąco. W takim razie jakie musiałbym wprowadzić zmiany w poniższym kodzie aby sortował właśnie malejąco?
void radixsort(int *input,int n)
{
int i;
// find the max number in the given list.
// to be used in loop termination part.
int maxNumber = input[0];
for (i = 1; i < n; i++)
{
if (input[i] > maxNumber)
maxNumber = input[i];
}
// run the loop for each of the decimal places
int exp = 1;
int *tmpBuffer = new int[n];
while (maxNumber / exp > 0)
{
int decimalBucket[10] = { 0 };
// count the occurences in this decimal digit.
for (i = 0; i < n; i++)
decimalBucket[input[i] / exp % 10]++;
// Prepare the position counters to be used for re-ordering the numbers
// for this decimal place.
for (i = 1; i < 10; i++)
decimalBucket[i] += decimalBucket[i - 1];
// Re order the numbers in the tmpbuffer and later copy back to original buffer.
for (i = n - 1; i >= 0; i--)
tmpBuffer[--decimalBucket[input[i] / exp % 10]] = input[i];
for (i = 0; i < n; i++)
input[i] = tmpBuffer[i];
// Move to next decimal place.
exp *= 10;
}
}
Z góry dziękuje za pomoc!