Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 18, 2015 20:10
Show Gist options
  • Save lettergram/39da091bfe634af655d8 to your computer and use it in GitHub Desktop.
Save lettergram/39da091bfe634af655d8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void printArray(int * array, int size){
int i;
printf("[ ");
for (i = 0; i < size; i++)
printf("%d ", array[i]);
printf("]\n");
}
int findLargestNum(int * array, int size){
int i;
int largestNum = -1;
for(i = 0; i < size; i++){
if(array[i] > largestNum)
largestNum = array[i];
}
return largestNum;
}
// Radix Sort
void radixSort(int * array, int size){
printf("\n\nRunning Radix Sort on Unsorted List!\n\n");
// Base 10 is used
int i;
int semiSorted[size];
int significantDigit = 1;
int largestNum = findLargestNum(array, size);
// Loop until we reach the largest significant digit
while (largestNum / significantDigit > 0){
printf("\tSorting: %d's place ", significantDigit);
printArray(array, size);
int bucket[10] = { 0 };
// Counts the number of "keys" or digits that will go into each bucket
for (i = 0; i < size; i++)
bucket[(array[i] / significantDigit) % 10]++;
/**
* Add the count of the previous buckets,
* Acquires the indexes after the end of each bucket location in the array
* Works similar to the count sort algorithm
**/
for (i = 1; i < 10; i++)
bucket[i] += bucket[i - 1];
// Use the bucket to fill a "semiSorted" array
for (i = size - 1; i >= 0; i--)
semiSorted[--bucket[(array[i] / significantDigit) % 10]] = array[i];
for (i = 0; i < size; i++)
array[i] = semiSorted[i];
// Move to next significant digit
significantDigit *= 10;
printf("\n\tBucket: ");
printArray(bucket, 10);
}
}
int main(){
printf("\n\nRunning Radix Sort Example in C!\n");
printf("----------------------------------\n");
int size = 12;
int list[] = {10, 2, 303, 4021, 293, 1, 0, 429, 480, 92, 2999, 14};
printf("\nUnsorted List: ");
printArray(&list[0], size);
radixSort(&list[0], size);
printf("\nSorted List:");
printArray(&list[0], size);
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment