diff --git a/Radix-sort b/Radix-sort new file mode 100644 index 0000000..783e9c8 --- /dev/null +++ b/Radix-sort @@ -0,0 +1,70 @@ +#include +using namespace std; + +// A utility function to get maximum value in arr[] +int getMax(int arr[], int size) +{ + int max = arr[0]; + for (int i = 1; i < size; i++) + if (arr[i] > max) + max = arr[i]; + return max; +} + +void CountingSort(int arr[], int size, int div) +{ + int output[size]; + int count[10] = {0}; + + for (int i = 0; i < size; i++) + count[ (arr[i]/div)%10 ]++; + + for (int i = 1; i < 10; i++) + count[i] += count[i - 1]; + + for (int i = size - 1; i >= 0; i--) + { + output[count[ (arr[i]/div)%10 ] - 1] = arr[i]; + count[ (arr[i]/div)%10 ]--; + } + + for (int i = 0; i < size; i++) + arr[i] = output[i]; +} + + +void RadixSort(int arr[], int size) +{ + int m = getMax(arr, size); + for (int div = 1; m/div > 0; div *= 10) + CountingSort(arr, size, div); +} + + +int main() +{ + int size; + cout<<"Enter the size of the array: "<>size; + int arr[size]; + cout<<"Enter "<>arr[i]; + } + cout<