From 231c99f8802346b84962bb087f37ed34d1a0d3fc Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 13:02:10 -0400 Subject: [PATCH] fix dynamic array issues in sorting folder (cherry picked from commit 01b69fcb249c30d4f1795766bf4cc617d8200ab3) --- sorting/comb_sort.cpp | 1 + sorting/insertion_sort.cpp | 62 ++++++++++---------- sorting/merge_sort.cpp | 66 +++++++++------------ sorting/quick_sort.cpp | 51 +++++++--------- sorting/radix_sort.cpp | 116 +++++++++++++++++-------------------- sorting/shell_sort.cpp | 72 ++++++++++------------- sorting/slow_sort.cpp | 82 +++++++++++++------------- sorting/swap_sort.cpp | 7 ++- sorting/tim_sort.cpp | 110 ++++++++++++++++------------------- 9 files changed, 256 insertions(+), 311 deletions(-) diff --git a/sorting/comb_sort.cpp b/sorting/comb_sort.cpp index feed4ba44..1b0a4d706 100644 --- a/sorting/comb_sort.cpp +++ b/sorting/comb_sort.cpp @@ -2,6 +2,7 @@ // While Bubble sort is comparering adjacent value, Combsort is using gap larger // than 1 Best case: O(n) Worst case: O(n ^ 2) +#include #include int a[100005]; diff --git a/sorting/insertion_sort.cpp b/sorting/insertion_sort.cpp index 2563acaf8..fe920ca59 100644 --- a/sorting/insertion_sort.cpp +++ b/sorting/insertion_sort.cpp @@ -1,40 +1,36 @@ -//Insertion Sort +// Insertion Sort #include -using namespace std; -int main() -{ - int n; - cout << "\nEnter the length of your array : "; - cin >> n; - int Array[n]; - cout << "\nEnter any " << n << " Numbers for Unsorted Array : "; +int main() { + int n; + std::cout << "\nEnter the length of your array : "; + std::cin >> n; + int *Array = new int[n]; + std::cout << "\nEnter any " << n << " Numbers for Unsorted Array : "; - //Input - for (int i = 0; i < n; i++) - { - cin >> Array[i]; - } + // Input + for (int i = 0; i < n; i++) { + std::cin >> Array[i]; + } - //Sorting - for (int i = 1; i < n; i++) - { - int temp = Array[i]; - int j = i - 1; - while (j >= 0 && temp < Array[j]) - { - Array[j + 1] = Array[j]; - j--; - } - Array[j + 1] = temp; - } + // Sorting + for (int i = 1; i < n; i++) { + int temp = Array[i]; + int j = i - 1; + while (j >= 0 && temp < Array[j]) { + Array[j + 1] = Array[j]; + j--; + } + Array[j + 1] = temp; + } - //Output - cout << "\nSorted Array : "; - for (int i = 0; i < n; i++) - { - cout << Array[i] << "\t"; - } - return 0; + // Output + std::cout << "\nSorted Array : "; + for (int i = 0; i < n; i++) { + std::cout << Array[i] << "\t"; + } + + delete[] Array; + return 0; } diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index b8edc8851..82ab869cd 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -1,57 +1,47 @@ #include -using namespace std; -void merge(int arr[], int l, int m, int r) -{ +void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; - int L[n1], R[n2]; + int *L = new int[n1], *R = new int[n2]; - for (i = 0; i < n1; i++) - L[i] = arr[l + i]; - for (j = 0; j < n2; j++) - R[j] = arr[m + 1 + j]; + for (i = 0; i < n1; i++) L[i] = arr[l + i]; + for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; - while (i < n1 && j < n2) - { - if (L[i] <= R[j]) - { + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { arr[k] = L[i]; i++; - } - else - { + } else { arr[k] = R[j]; j++; } k++; } - while (i < n1) - { + while (i < n1) { arr[k] = L[i]; i++; k++; } - while (j < n2) - { + while (j < n2) { arr[k] = R[j]; j++; k++; } + + delete[] L; + delete[] R; } -void mergeSort(int arr[], int l, int r) -{ - if (l < r) - { - +void mergeSort(int arr[], int l, int r) { + if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); @@ -61,33 +51,31 @@ void mergeSort(int arr[], int l, int r) } } -void show(int A[], int size) -{ +void show(int A[], int size) { int i; - for (i = 0; i < size; i++) - cout << A[i] << "\n"; + for (i = 0; i < size; i++) std::cout << A[i] << "\n"; } -int main() -{ +int main() { int size; - cout << "\nEnter the number of elements : "; + std::cout << "\nEnter the number of elements : "; - cin >> size; + std::cin >> size; - int arr[size]; + int *arr = new int[size]; - cout << "\nEnter the unsorted elements : "; + std::cout << "\nEnter the unsorted elements : "; - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; + for (int i = 0; i < size; ++i) { + std::cout << "\n"; + std::cin >> arr[i]; } mergeSort(arr, 0, size); - cout << "Sorted array\n"; + std::cout << "Sorted array\n"; show(arr, size); + + delete[] arr; return 0; } diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 0b807898f..489e10965 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -1,19 +1,15 @@ /* C implementation QuickSort */ #include -using namespace std; -int partition(int arr[], int low, int high) -{ - int pivot = arr[high]; // pivot - int i = (low - 1); // Index of smaller element +int partition(int arr[], int low, int high) { + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element - for (int j = low; j < high; j++) - { + for (int j = low; j < high; j++) { // If current element is smaller than or // equal to pivot - if (arr[j] <= pivot) - { - i++; // increment index of smaller element + if (arr[j] <= pivot) { + i++; // increment index of smaller element int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; @@ -25,11 +21,8 @@ int partition(int arr[], int low, int high) return (i + 1); } -void quickSort(int arr[], int low, int high) -{ - if (low < high) - { - +void quickSort(int arr[], int low, int high) { + if (low < high) { int p = partition(arr, low, high); quickSort(arr, low, p - 1); @@ -37,31 +30,29 @@ void quickSort(int arr[], int low, int high) } } -void show(int arr[], int size) -{ - for (int i = 0; i < size; i++) - cout << arr[i] << "\n"; +void show(int arr[], int size) { + for (int i = 0; i < size; i++) std::cout << arr[i] << "\n"; } // Driver program to test above functions -int main() -{ +int main() { int size; - cout << "\nEnter the number of elements : "; + std::cout << "\nEnter the number of elements : "; - cin >> size; + std::cin >> size; - int arr[size]; + int *arr = new int[size]; - cout << "\nEnter the unsorted elements : "; + std::cout << "\nEnter the unsorted elements : "; - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; + for (int i = 0; i < size; ++i) { + std::cout << "\n"; + std::cin >> arr[i]; } quickSort(arr, 0, size); - cout << "Sorted array\n"; + std::cout << "Sorted array\n"; show(arr, size); + + delete[] arr; return 0; } diff --git a/sorting/radix_sort.cpp b/sorting/radix_sort.cpp index 09c91bb22..e5bc3db84 100644 --- a/sorting/radix_sort.cpp +++ b/sorting/radix_sort.cpp @@ -1,68 +1,58 @@ -#include -#include #include +#include #include -using namespace std; -void radixsort(int a[], int n) -{ - int count[10]; - int output[n]; - memset(output, 0, sizeof(output)); - memset(count, 0, sizeof(count)); - int max = 0; - for (int i = 0; i < n; ++i) - { - if (a[i] > max) - { - max = a[i]; - } - } - int maxdigits = 0; - while (max) - { - maxdigits++; - max /= 10; - } - for (int j = 0; j < maxdigits; j++) - { - for (int i = 0; i < n; i++) - { - int t = pow(10, j); - count[(a[i] % (10 * t)) / t]++; - } - int k = 0; - for (int p = 0; p < 10; p++) - { - for (int i = 0; i < n; i++) - { - int t = pow(10, j); - if ((a[i] % (10 * t)) / t == p) - { - output[k] = a[i]; - k++; - } - } - } - memset(count, 0, sizeof(count)); - for (int i = 0; i < n; ++i) - { - a[i] = output[i]; - } - } +#include + +void radixsort(int a[], int n) { + int count[10]; + int* output = new int[n]; + memset(output, 0, n * sizeof(*output)); + memset(count, 0, sizeof(count)); + int max = 0; + for (int i = 0; i < n; ++i) { + if (a[i] > max) { + max = a[i]; + } + } + int maxdigits = 0; + while (max) { + maxdigits++; + max /= 10; + } + for (int j = 0; j < maxdigits; j++) { + for (int i = 0; i < n; i++) { + int t = std::pow(10, j); + count[(a[i] % (10 * t)) / t]++; + } + int k = 0; + for (int p = 0; p < 10; p++) { + for (int i = 0; i < n; i++) { + int t = std::pow(10, j); + if ((a[i] % (10 * t)) / t == p) { + output[k] = a[i]; + k++; + } + } + } + memset(count, 0, sizeof(count)); + for (int i = 0; i < n; ++i) { + a[i] = output[i]; + } + } + delete[] output; } -void print(int a[], int n) -{ - for (int i = 0; i < n; ++i) - { - cout << a[i] << " "; - } - cout << endl; + +void print(int a[], int n) { + for (int i = 0; i < n; ++i) { + std::cout << a[i] << " "; + } + std::cout << std::endl; } -int main(int argc, char const *argv[]) -{ - int a[] = {170, 45, 75, 90, 802, 24, 2, 66}; - int n = sizeof(a) / sizeof(a[0]); - radixsort(a, n); - print(a, n); - return 0; + +int main(int argc, char const* argv[]) { + int a[] = {170, 45, 75, 90, 802, 24, 2, 66}; + int n = sizeof(a) / sizeof(a[0]); + radixsort(a, n); + print(a, n); + return 0; } \ No newline at end of file diff --git a/sorting/shell_sort.cpp b/sorting/shell_sort.cpp index b08c7ffd8..eb701478d 100644 --- a/sorting/shell_sort.cpp +++ b/sorting/shell_sort.cpp @@ -1,45 +1,37 @@ #include -using namespace std; -int main() -{ - int size = 10; - int array[size]; - // Input - cout << "\nHow many numbers do want to enter in unsorted array : "; - cin >> size; - cout << "\nEnter the numbers for unsorted array : "; - for (int i = 0; i < size; i++) - { - cin >> array[i]; - } +int main() { + int size = 10; + int* array = new int[size]; + // Input + std::cout << "\nHow many numbers do want to enter in unsorted array : "; + std::cin >> size; + std::cout << "\nEnter the numbers for unsorted array : "; + for (int i = 0; i < size; i++) { + std::cin >> array[i]; + } - // Sorting - for (int i = size / 2; i > 0; i = i / 2) - { - for (int j = i; j < size; j++) - { - for (int k = j - i; k >= 0; k = k - i) - { - if (array[k] < array[k + i]) - { - break; - } - else - { - int temp = array[k + i]; - array[k + i] = array[k]; - array[k] = temp; - } - } - } - } + // Sorting + for (int i = size / 2; i > 0; i = i / 2) { + for (int j = i; j < size; j++) { + for (int k = j - i; k >= 0; k = k - i) { + if (array[k] < array[k + i]) { + break; + } else { + int temp = array[k + i]; + array[k + i] = array[k]; + array[k] = temp; + } + } + } + } - // Output - cout << "\nSorted array : "; - for (int i = 0; i < size; ++i) - { - cout << array[i] << "\t"; - } - return 0; + // Output + std::cout << "\nSorted array : "; + for (int i = 0; i < size; ++i) { + std::cout << array[i] << "\t"; + } + + delete[] array; + return 0; } diff --git a/sorting/slow_sort.cpp b/sorting/slow_sort.cpp index f8f7e74b8..2872b37d8 100644 --- a/sorting/slow_sort.cpp +++ b/sorting/slow_sort.cpp @@ -1,57 +1,55 @@ -//Returns the sorted vector after performing SlowSort -//It is a sorting algorithm that is of humorous nature and not useful. -//It's based on the principle of multiply and surrender, a tongue-in-cheek joke of divide and conquer. -//It was published in 1986 by Andrei Broder and Jorge Stolfi in their paper Pessimal Algorithms and Simplexity Analysis. -//This algorithm multiplies a single problem into multiple subproblems -//It is interesting because it is provably the least efficient sorting algorithm that can be built asymptotically, -//and with the restriction that such an algorithm, while being slow, must still all the time be working towards a result. +// Returns the sorted vector after performing SlowSort +// It is a sorting algorithm that is of humorous nature and not useful. +// It's based on the principle of multiply and surrender, a tongue-in-cheek joke +// of divide and conquer. It was published in 1986 by Andrei Broder and Jorge +// Stolfi in their paper Pessimal Algorithms and Simplexity Analysis. This +// algorithm multiplies a single problem into multiple subproblems It is +// interesting because it is provably the least efficient sorting algorithm that +// can be built asymptotically, and with the restriction that such an algorithm, +// while being slow, must still all the time be working towards a result. #include using namespace std; -void SlowSort(int a[], int i, int j) -{ - if (i >= j) - return; - int m = i + (j - i) / 2; //midpoint, implemented this way to avoid overflow - int temp; - SlowSort(a, i, m); - SlowSort(a, m + 1, j); - if (a[j] < a[m]) - { - temp = a[j]; //swapping a[j] & a[m] - a[j] = a[m]; - a[m] = temp; - } - SlowSort(a, i, j - 1); +void SlowSort(int a[], int i, int j) { + if (i >= j) return; + int m = i + (j - i) / 2; // midpoint, implemented this way to avoid + // overflow + int temp; + SlowSort(a, i, m); + SlowSort(a, m + 1, j); + if (a[j] < a[m]) { + temp = a[j]; // swapping a[j] & a[m] + a[j] = a[m]; + a[m] = temp; + } + SlowSort(a, i, j - 1); } -//Sample Main function +// Sample Main function -int main() -{ - int size; - cout << "\nEnter the number of elements : "; +int main() { + int size; + cout << "\nEnter the number of elements : "; - cin >> size; + cin >> size; - int arr[size]; + int *arr = new int[size]; - cout << "\nEnter the unsorted elements : "; + cout << "\nEnter the unsorted elements : "; - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; - } + for (int i = 0; i < size; ++i) { + cout << "\n"; + cin >> arr[i]; + } - SlowSort(arr, 0, size); + SlowSort(arr, 0, size); - cout << "Sorted array\n"; + cout << "Sorted array\n"; - for (int i = 0; i < size; ++i) - { - cout << arr[i] << " "; - } - return 0; + for (int i = 0; i < size; ++i) { + cout << arr[i] << " "; + } + delete[] arr; + return 0; } diff --git a/sorting/swap_sort.cpp b/sorting/swap_sort.cpp index a4ab1e57b..2e59e2fb3 100644 --- a/sorting/swap_sort.cpp +++ b/sorting/swap_sort.cpp @@ -10,7 +10,7 @@ int minSwaps(int arr[], int n) { // Create an array of pairs where first // element is array element and second element // is position of first element - std::pair arrPos[n]; + std::pair *arrPos = new std::pair[n]; for (int i = 0; i < n; i++) { arrPos[i].first = arr[i]; arrPos[i].second = i; @@ -32,8 +32,7 @@ int minSwaps(int arr[], int n) { for (int i = 0; i < n; i++) { // already swapped and corrected or // already present at correct pos - if (vis[i] || arrPos[i].second == i) - continue; + if (vis[i] || arrPos[i].second == i) continue; // find out the number of node in // this cycle and add in ans @@ -53,6 +52,8 @@ int minSwaps(int arr[], int n) { } } + delete[] arrPos; + // Return result return ans; } diff --git a/sorting/tim_sort.cpp b/sorting/tim_sort.cpp index 14d3a04d0..94f5aa230 100644 --- a/sorting/tim_sort.cpp +++ b/sorting/tim_sort.cpp @@ -1,115 +1,103 @@ // C++ program to perform TimSort. +#include #include -using namespace std; + const int RUN = 32; - -// this function sorts array from left index to to right index which is of size atmost RUN -void insertionSort(int arr[], int left, int right) -{ - for (int i = left + 1; i <= right; i++) - { + +// this function sorts array from left index to to right index which is of size +// atmost RUN +void insertionSort(int arr[], int left, int right) { + for (int i = left + 1; i <= right; i++) { int temp = arr[i]; int j = i - 1; - while (arr[j] > temp && j >= left) - { - arr[j+1] = arr[j]; + while (arr[j] > temp && j >= left) { + arr[j + 1] = arr[j]; j--; } - arr[j+1] = temp; + arr[j + 1] = temp; } } - + // merge function merges the sorted runs -void merge(int arr[], int l, int m, int r) -{ +void merge(int arr[], int l, int m, int r) { // original array is broken in two parts, left and right array int len1 = m - l + 1, len2 = r - m; - int left[len1], right[len2]; - for (int i = 0; i < len1; i++) - left[i] = arr[l + i]; - for (int i = 0; i < len2; i++) - right[i] = arr[m + 1 + i]; - + int *left = new int[len1], *right = new int[len2]; + for (int i = 0; i < len1; i++) left[i] = arr[l + i]; + for (int i = 0; i < len2; i++) right[i] = arr[m + 1 + i]; + int i = 0; int j = 0; int k = l; - + // after comparing, we merge those two array in larger sub array - while (i < len1 && j < len2) - { - if (left[i] <= right[j]) - { + while (i < len1 && j < len2) { + if (left[i] <= right[j]) { arr[k] = left[i]; i++; - } - else - { + } else { arr[k] = right[j]; j++; } k++; } - + // copy remaining elements of left, if any - while (i < len1) - { + while (i < len1) { arr[k] = left[i]; k++; i++; } - + // copy remaining element of right, if any - while (j < len2) - { + while (j < len2) { arr[k] = right[j]; k++; j++; } + delete[] left; + delete[] right; } - + // iterative Timsort function to sort the array[0...n-1] (similar to merge sort) -void timSort(int arr[], int n) -{ +void timSort(int arr[], int n) { // Sort individual subarrays of size RUN - for (int i = 0; i < n; i+=RUN) - insertionSort(arr, i, min((i+31), (n-1))); - - // start merging from size RUN (or 32). It will merge to form size 64, then 128, 256 and so on .... - for (int size = RUN; size < n; size = 2*size) - { - // pick starting point of left sub array. We are going to merge arr[left..left+size-1] and arr[left+size, left+2*size-1] - // After every merge, we increase left by 2*size - for (int left = 0; left < n; left += 2*size) - { + for (int i = 0; i < n; i += RUN) + insertionSort(arr, i, std::min((i + 31), (n - 1))); + + // start merging from size RUN (or 32). It will merge to form size 64, then + // 128, 256 and so on .... + for (int size = RUN; size < n; size = 2 * size) { + // pick starting point of left sub array. We are going to merge + // arr[left..left+size-1] and arr[left+size, left+2*size-1] After every + // merge, we increase left by 2*size + for (int left = 0; left < n; left += 2 * size) { // find ending point of left sub array // mid+1 is starting point of right sub array int mid = left + size - 1; - int right = min((left + 2*size - 1), (n-1)); - + int right = std::min((left + 2 * size - 1), (n - 1)); + // merge sub array arr[left.....mid] & arr[mid+1....right] merge(arr, left, mid, right); } } } - + // utility function to print the Array -void printArray(int arr[], int n) -{ - for (int i = 0; i < n; i++) - printf("%d ", arr[i]); - printf("\n"); +void printArray(int arr[], int n) { + for (int i = 0; i < n; i++) printf("%d ", arr[i]); + std::cout << std::endl; } - + // Driver program to test above function -int main() -{ +int main() { int arr[] = {5, 21, 7, 23, 19}; - int n = sizeof(arr)/sizeof(arr[0]); + int n = sizeof(arr) / sizeof(arr[0]); printf("Given Array is\n"); printArray(arr, n); - + timSort(arr, n); - + printf("After Sorting Array is\n"); printArray(arr, n); return 0;