From 73fabe60e7358be7169329f69ac0b3e6e15ac288 Mon Sep 17 00:00:00 2001 From: abdurrezzak Date: Sun, 29 Oct 2017 14:48:21 +0200 Subject: [PATCH] Add files via upload --- 10.RadixSort.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 10.RadixSort.cpp diff --git a/10.RadixSort.cpp b/10.RadixSort.cpp new file mode 100644 index 0000000..bb3c960 --- /dev/null +++ b/10.RadixSort.cpp @@ -0,0 +1,54 @@ +/* + * This program takes an array from the user + * sorts it using radix sort + * Prints the sorted array + * For more information about Radix Sort Algorithm: https://en.wikipedia.org/wiki/Counting_sort + * + * Coded by: Abdurrezak EFE + * + * */ +#include +#include +using namespace std; + +void counting_sort(int arr[], int k, int power) +{ + int counts[11] = {0}; //initializing count array + + for(int i=0; i=0; i--) //change the order of the original array + arr[counts[(arr2[i]/power)%10]-1] = arr2[i], counts[(arr[i]/power)%10]--; +} + +void radix_sort(int arr[], int k, int maxx) +{ + for (int power = 1; maxx/power > 0; power *= 10) //sorting on each digit + counting_sort(arr, k, power); +} + +int main() +{ + int k; + cout << "Enter the number of the integers you want to construct the array from: "; + cin >> k; + + int arr[k]; + int maxx = -1; + for(int i=0;i> arr[i], maxx = max(maxx,arr[i]); + + radix_sort(arr, k, maxx); //sorting + + for(int i=0;i