From 76a5f572d5d9834585201836183027e3575b1e55 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 11:46:24 -0400 Subject: [PATCH] sorting fixes for MSVC and CPPLINT --- sorting/bead_sort.cpp | 5 ++-- sorting/bucket_sort.cpp | 51 ++++++++++++++++++----------------------- sorting/comb_sort.cpp | 47 +++++++++++++++---------------------- 3 files changed, 43 insertions(+), 60 deletions(-) diff --git a/sorting/bead_sort.cpp b/sorting/bead_sort.cpp index 12b7d4d32..d34706073 100644 --- a/sorting/bead_sort.cpp +++ b/sorting/bead_sort.cpp @@ -12,8 +12,8 @@ void beadSort(int *a, int len) { if (a[i] > max) max = a[i]; // allocating memory - unsigned char beads[max * len]; - memset(beads, 0, sizeof(beads)); + unsigned char *beads = new unsigned char[max * len]; + memset(beads, 0, max * len); // mark the beads for (int i = 0; i < len; i++) @@ -39,6 +39,7 @@ void beadSort(int *a, int len) { a[i] = j; } + delete[] beads; } // driver function to test the algorithm diff --git a/sorting/bucket_sort.cpp b/sorting/bucket_sort.cpp index 0ffbf8e4c..f120f04fc 100644 --- a/sorting/bucket_sort.cpp +++ b/sorting/bucket_sort.cpp @@ -1,42 +1,35 @@ // C++ program to sort an array using bucket sort -#include #include +#include #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]; +void bucketSort(float arr[], int n) { + // 1) Create n empty buckets + std::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]); - } + // 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 - for (int i = 0; i < n; i++) - sort(b[i].begin(), b[i].end()); + // 3) Sort individual buckets + for (int i = 0; i < n; i++) std::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]; + // 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 funtion */ -int main() -{ - float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; - int n = sizeof(arr) / sizeof(arr[0]); - bucketSort(arr, n); +int main() { + float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}; + 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] << " "; - return 0; + std::cout << "Sorted array is \n"; + for (int i = 0; i < n; i++) std::cout << arr[i] << " "; + return 0; } diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index 2209c96fc..feed4ba44 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -1,59 +1,48 @@ -//Kind of better version of Bubble sort. -//While Bubble sort is comparering adjacent value, Combsort is using gap larger than 1 -//Best case: O(n) -//Worst case: O(n ^ 2) +// Kind of better version of Bubble sort. +// While Bubble sort is comparering adjacent value, Combsort is using gap larger +// than 1 Best case: O(n) Worst case: O(n ^ 2) #include -using namespace std; - int a[100005]; int n; -int FindNextGap(int x) -{ +int FindNextGap(int x) { x = (x * 10) / 13; - return max(1, x); + return std::max(1, x); } -void CombSort(int a[], int l, int r) -{ - //Init gap +void CombSort(int a[], int l, int r) { + // Init gap int gap = n; - //Initialize swapped as true to make sure that loop runs + // Initialize swapped as true to make sure that loop runs bool swapped = true; - //Keep running until gap = 1 or none elements were swapped - while (gap != 1 || swapped) - { - //Find next gap + // Keep running until gap = 1 or none elements were swapped + while (gap != 1 || swapped) { + // Find next gap gap = FindNextGap(gap); swapped = false; // Compare all elements with current gap - for (int i = l; i <= r - gap; ++i) - { - if (a[i] > a[i + gap]) - { - swap(a[i], a[i + gap]); + for (int i = l; i <= r - gap; ++i) { + if (a[i] > a[i + gap]) { + std::swap(a[i], a[i + gap]); swapped = true; } } } } -int main() -{ - cin >> n; - for (int i = 1; i <= n; ++i) - cin >> a[i]; +int main() { + std::cin >> n; + for (int i = 1; i <= n; ++i) std::cin >> a[i]; CombSort(a, 1, n); - for (int i = 1; i <= n; ++i) - cout << a[i] << ' '; + for (int i = 1; i <= n; ++i) std::cout << a[i] << ' '; return 0; }