Hi,
Can anyone tell me if "counting sort" can be made to sort in desending order?
thnx
Hi,
Can anyone tell me if "counting sort" can be made to sort in desending order?
thnx
Of course:
-PreludeCode:static void counting_sort ( int *array, int size ) { int *counter; int i, j, k = size - 1; int max = array[0]; for ( i = 0; i < size; i++ ) { if ( array[i] > max ) max = array[i]; } counter = malloc ( ( max + 1 ) * sizeof *counter ); if ( counter == NULL ) return; for ( i = 0; i < max; i++ ) counter[i] = 0; for ( i = 0; i < size; i++ ) counter[array[i]]++; for ( i = 0; i < max + 1; i++ ) { for ( j = 0; j < counter[i]; j++ ) array[k--] = i; } free ( counter ); }
Last edited by Prelude; 03-06-2003 at 09:59 AM.
My best code is written with the delete key.
But how is your version descending? It looks different from the one on http://www.cs.cf.ac.uk/user/C.L.Mumf...ountPage.html.
thnx
>But how is your version descending?
Instead of copying from 0 to size-1 into array, you do the reverse for a descending sort.
-Prelude
My best code is written with the delete key.