From fbac32b164b72f04935783337171a6260cb363ee Mon Sep 17 00:00:00 2001 From: Nishant Singh <70948353+GamerNishant@users.noreply.github.com> Date: Fri, 16 Oct 2020 18:26:58 +0530 Subject: [PATCH] adding RadixSort.cpp adding RadixSort algorithm to master branch. --- GamerNishant_RadixSort.cpp.cpp | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 GamerNishant_RadixSort.cpp.cpp diff --git a/GamerNishant_RadixSort.cpp.cpp b/GamerNishant_RadixSort.cpp.cpp new file mode 100644 index 000000000..bf7474779 --- /dev/null +++ b/GamerNishant_RadixSort.cpp.cpp @@ -0,0 +1,60 @@ +/* Author - Rahul Pathak */ +/* Given n d-digit numbers in which each digit can take on up to k possible values, + RADIX-SORT correctly sorts these numbers in O(d*(n+k)) time if the stable sort + it uses takes O(n+k) time. + Here, we use Counting sort as a subroutine. +*/ + +/* For each digit i where i varies from the least significant to the most significant digit, + sort the input array using counting sort according to the i'th digit. */ +#include +#include +using namespace std; + +int findMaxElement(vector A) +{ + int maximum = A[0]; + for (int i = 1; i < A.size(); i++) + { + maximum = max(maximum, A[i]); + } + return maximum; +} + +void radixSort(vector &A) +{ + int power, i = findMaxElement(A); + // Count sort subroutine for each digit + // This loops iterates for number of times equal to the number of digits in the maximum element + for (power = 1; i / power != 0; power *= 10) + { + int result[A.size()], i, count[10] = {0}; + + // Store frequency + for (i = 0; i < A.size(); i++) + count[(A[i] / power) % 10]++; + // Store number of elements before the current element + // that should appear in the sorted array + for (i = 1; i < 10; i++) + count[i] += count[i - 1]; + // placing each element at correct place in the array sorted according to + // the particular digit + for (i = A.size() - 1; i >= 0; i--) + { + result[count[(A[i] / power) % 10] - 1] = A[i]; + count[(A[i] / power) % 10]--; + } + for (i = 0; i < A.size(); i++) + A[i] = result[i]; + } +} + +int main() +{ + vector A = {23, 2, 43, 32, 8, 13, 5, 16, 17, 1}; + radixSort(A); + for (int i = 0; i < A.size(); i++) + cout << A[i] << ' '; + cout << endl; + return 0; +} \ No newline at end of file