From ce1fdc017242a1fa45f770d520eb9226719d7155 Mon Sep 17 00:00:00 2001 From: SankalpGupta Date: Thu, 1 Oct 2020 12:29:36 +0530 Subject: [PATCH 1/4] add radixsort.cpp in cpp --- Cpp/RadixSort.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Cpp/RadixSort.cpp diff --git a/Cpp/RadixSort.cpp b/Cpp/RadixSort.cpp new file mode 100644 index 0000000..bf70417 --- /dev/null +++ b/Cpp/RadixSort.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +// A function to do counting sort of arr[] according to +// the digit represented by exp. +void CountSort(int arr[], int n, int exp) +{ + int output[n]; // output array + int i, count[10] = {0}; + + // Store count of occurrences in count[] + for (i = 0; i < n; i++) + count[ (arr[i]/exp)%10 ]++; + + // Change count[i] so that count[i] now contains actual + // position of this digit in output[] + for (i = 1; i < 10; i++) + count[i] += count[i - 1]; + + // Build the output array + for (i = n - 1; i >= 0; i--) + { + output[count[ (arr[i]/exp)%10 ] - 1] = arr[i]; + count[ (arr[i]/exp)%10 ]--; + } + + // Copy the output array to arr[], so that arr[] now + // contains sorted numbers according to current digit + for (i = 0; i < n; i++) + arr[i] = output[i]; +} + +// The main function to that sorts arr[] of size n using +// Radix Sort +void RadixSort(int arr[], int n) +{ + // Find the maximum number to know number of digits + int m = arr[0]; + + for(int i = 1; i < n; i++){ + m = max(m,arr[i]); // gives max of 2 elements + } + // Do counting sort for every digit. Note that instead + // of passing digit number, exp is passed. exp is 10^i + // where i is current digit number + for (int exp = 1; m/exp > 0; exp *= 10) + CountSort(arr, n, exp); +} + + +// Driver program to test above functions +int main() +{ + int arr[] = {17, 45, 75, 90, 802, 24, 2, 66}; + int n = sizeof(arr)/sizeof(arr[0]); + RadixSort(arr, n); + + // print the result + for(int i = 0;i < n; i++) + cout << arr[i] << " "; + return 0; +} \ No newline at end of file From 2f3bddda659b9e7da4e19c57d5bc6714ef944926 Mon Sep 17 00:00:00 2001 From: SankalpGupta Date: Thu, 1 Oct 2020 12:53:25 +0530 Subject: [PATCH 2/4] added BucketSort.cpp in Cpp --- Cpp/BucketSort.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Cpp/BucketSort.cpp diff --git a/Cpp/BucketSort.cpp b/Cpp/BucketSort.cpp new file mode 100644 index 0000000..e3eba7e --- /dev/null +++ b/Cpp/BucketSort.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +// Function to sort arr[] of size n using bucket sort +void bucketSort(float arr[], int n) +{ + // 1) Create n empty buckets + vector b[n]; + + // 2) Put array elements in different buckets + for (int i = 0; i < n; i++) { + int bi = n * arr[i]; // Index in bucket + b[bi].push_back(arr[i]); + } + + // 3) Sort individual buckets using stl sort + for (int i = 0; i < n; i++) + sort(b[i].begin(), b[i].end()); + + // 4) Concatenate all buckets into arr[] + int index = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < b[i].size(); j++) + arr[index++] = b[i][j]; +} + +/* Driver program to test above function */ +int main() +{ + float arr[] = { 0.197, 0.1565, 0.6256, 0.1234, 0.557, 0.3434, 0.9987 }; + int n = sizeof(arr) / sizeof(arr[0]); + bucketSort(arr, n); + + cout << "Sorted array is \n"; + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + cout< Date: Thu, 1 Oct 2020 13:19:22 +0530 Subject: [PATCH 3/4] deleted bucketsort.cpp --- Cpp/BucketSort.cpp | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 Cpp/BucketSort.cpp diff --git a/Cpp/BucketSort.cpp b/Cpp/BucketSort.cpp deleted file mode 100644 index e3eba7e..0000000 --- a/Cpp/BucketSort.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include -using namespace std; - -// Function to sort arr[] of size n using bucket sort -void bucketSort(float arr[], int n) -{ - // 1) Create n empty buckets - vector b[n]; - - // 2) Put array elements in different buckets - for (int i = 0; i < n; i++) { - int bi = n * arr[i]; // Index in bucket - b[bi].push_back(arr[i]); - } - - // 3) Sort individual buckets using stl sort - for (int i = 0; i < n; i++) - sort(b[i].begin(), b[i].end()); - - // 4) Concatenate all buckets into arr[] - int index = 0; - for (int i = 0; i < n; i++) - for (int j = 0; j < b[i].size(); j++) - arr[index++] = b[i][j]; -} - -/* Driver program to test above function */ -int main() -{ - float arr[] = { 0.197, 0.1565, 0.6256, 0.1234, 0.557, 0.3434, 0.9987 }; - int n = sizeof(arr) / sizeof(arr[0]); - bucketSort(arr, n); - - cout << "Sorted array is \n"; - for (int i = 0; i < n; i++) - cout << arr[i] << " "; - cout< Date: Wed, 6 Oct 2021 15:15:46 +0530 Subject: [PATCH 4/4] Delete RadixSort.cpp --- Cpp/RadixSort.cpp | 62 ----------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 Cpp/RadixSort.cpp diff --git a/Cpp/RadixSort.cpp b/Cpp/RadixSort.cpp deleted file mode 100644 index bf70417..0000000 --- a/Cpp/RadixSort.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include -using namespace std; - -// A function to do counting sort of arr[] according to -// the digit represented by exp. -void CountSort(int arr[], int n, int exp) -{ - int output[n]; // output array - int i, count[10] = {0}; - - // Store count of occurrences in count[] - for (i = 0; i < n; i++) - count[ (arr[i]/exp)%10 ]++; - - // Change count[i] so that count[i] now contains actual - // position of this digit in output[] - for (i = 1; i < 10; i++) - count[i] += count[i - 1]; - - // Build the output array - for (i = n - 1; i >= 0; i--) - { - output[count[ (arr[i]/exp)%10 ] - 1] = arr[i]; - count[ (arr[i]/exp)%10 ]--; - } - - // Copy the output array to arr[], so that arr[] now - // contains sorted numbers according to current digit - for (i = 0; i < n; i++) - arr[i] = output[i]; -} - -// The main function to that sorts arr[] of size n using -// Radix Sort -void RadixSort(int arr[], int n) -{ - // Find the maximum number to know number of digits - int m = arr[0]; - - for(int i = 1; i < n; i++){ - m = max(m,arr[i]); // gives max of 2 elements - } - // Do counting sort for every digit. Note that instead - // of passing digit number, exp is passed. exp is 10^i - // where i is current digit number - for (int exp = 1; m/exp > 0; exp *= 10) - CountSort(arr, n, exp); -} - - -// Driver program to test above functions -int main() -{ - int arr[] = {17, 45, 75, 90, 802, 24, 2, 66}; - int n = sizeof(arr)/sizeof(arr[0]); - RadixSort(arr, n); - - // print the result - for(int i = 0;i < n; i++) - cout << arr[i] << " "; - return 0; -} \ No newline at end of file