From 030348a284415e04315b4ee865da47d7bce74fdd Mon Sep 17 00:00:00 2001 From: liushubin-gitHub <65377959+liushubin-gitHub@users.noreply.github.com> Date: Wed, 27 May 2020 10:03:54 +0800 Subject: [PATCH 1/5] Update doubly_linked_list.cpp when remove an item , should free the item memory --- data_structure/doubly_linked_list.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structure/doubly_linked_list.cpp b/data_structure/doubly_linked_list.cpp index 17ea2983a..4fc38abfc 100644 --- a/data_structure/doubly_linked_list.cpp +++ b/data_structure/doubly_linked_list.cpp @@ -61,6 +61,7 @@ void double_linked_list::remove(int x) { t->prev->next = t->next; t->next->prev = t->prev; } + delete t; } void double_linked_list::search(int x) { From ee3547fafcbf48650b0ae6eb92fe6a9809bb2b93 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Wed, 27 May 2020 15:41:15 +0530 Subject: [PATCH 2/5] Correction : Fixed Array Overflow --- sorting/quick_sort.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sorting/quick_sort.cpp diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp new file mode 100644 index 000000000..12ee66456 --- /dev/null +++ b/sorting/quick_sort.cpp @@ -0,0 +1,61 @@ +/* + * + * copyright The Algorithms + * Author - + * Correction - ayaankhan98 + * + */ + +#include +#include + +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++) { + // If current element is smaller than or + // equal to pivot + if (arr[j] <= pivot) { + i++; // increment index of smaller element + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + return (i + 1); +} + +void quickSort(int arr[], int low, int high) { + if (low < high) { + int p = partition(arr, low, high); + quickSort(arr, low, p - 1); + quickSort(arr, p + 1, high); + } +} + +void show(int arr[], int size) { + for (int i = 0; i < size; i++) + std::cout << arr[i] << " "; + std::cout << "\n"; +} + +// Driver program to test above functions + +int main() { + int size; + std::cout << "\nEnter the number of elements : "; + std::cin >> size; + int *arr = new int[size]; + std::cout << "\nEnter the unsorted elements : "; + for (int i = 0; i < size; ++i) { + std::cin >> arr[i]; + } + quickSort(arr, 0, size-1); + std::cout << "Sorted array : "; + show(arr, size); + return 0; +} From d86ff19d44ad919abb3f4cb64d9431300f5dded8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 27 May 2020 12:14:45 +0000 Subject: [PATCH 3/5] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e36309dc..636543a25 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -197,6 +197,7 @@ * [Numericstringsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/NumericStringSort.cpp) * [Oddeven Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/OddEven%20Sort.cpp) * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Quick%20Sort.cpp) + * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp) * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp) * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp) * [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp) From c2cde125dd8c9077a10c8a5b29dfd00933ec5adb Mon Sep 17 00:00:00 2001 From: Ayaan Khan <43348292+ayaankhan98@users.noreply.github.com> Date: Wed, 27 May 2020 18:54:19 +0530 Subject: [PATCH 4/5] Added documentation (#802) * Added documentation * Added breif comments on functions * modified comment block * further improved comment blocks --- sorting/Quick Sort.cpp | 67 ------------------------------------------ sorting/quick_sort.cpp | 38 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 70 deletions(-) delete mode 100644 sorting/Quick Sort.cpp diff --git a/sorting/Quick Sort.cpp b/sorting/Quick Sort.cpp deleted file mode 100644 index 0b807898f..000000000 --- a/sorting/Quick Sort.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* 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 - - 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 - int temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - } - } - int temp = arr[i + 1]; - arr[i + 1] = arr[high]; - arr[high] = temp; - return (i + 1); -} - -void quickSort(int arr[], int low, int high) -{ - if (low < high) - { - - int p = partition(arr, low, high); - - quickSort(arr, low, p - 1); - quickSort(arr, p + 1, high); - } -} - -void show(int arr[], int size) -{ - for (int i = 0; i < size; i++) - cout << arr[i] << "\n"; -} - -// Driver program to test above functions -int main() -{ - int size; - cout << "\nEnter the number of elements : "; - - cin >> size; - - int arr[size]; - - cout << "\nEnter the unsorted elements : "; - - for (int i = 0; i < size; ++i) - { - cout << "\n"; - cin >> arr[i]; - } - quickSort(arr, 0, size); - cout << "Sorted array\n"; - show(arr, size); - return 0; -} diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 12ee66456..d067fa068 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -1,16 +1,42 @@ -/* +/** * * copyright The Algorithms * Author - * Correction - ayaankhan98 * + * Implementation Details - + * Quick Sort is a divide and conquer algorithm. It picks and element as + * pivot and partition the given array around the picked pivot. There + * are many different versions of quickSort that pick pivot in different + * ways. + * + * 1. Always pick the first element as pivot + * 2. Always pick the last element as pivot (implemented below) + * 3. Pick a random element as pivot + * 4. Pick median as pivot + * + * The key process in quickSort is partition(). Target of partition is, + * given an array and an element x(say) of array as pivot, put x at it's + * correct position in sorted array and put all smaller elements (samller + * than x) before x, and put all greater elements (greater than x) after + * x. All this should be done in linear time + * */ #include #include +/** + * This function takes last element as pivot, places + * the pivot element at its correct position in sorted + * array, and places all smaller (smaller than pivot) + * to left of pivot and all greater elements to right + * of pivot + * + */ + int partition(int arr[], int low, int high) { - int pivot = arr[high]; // pivot + int pivot = arr[high]; // taking the last element as pivot int i = (low - 1); // Index of smaller element for (int j = low; j < high; j++) { @@ -29,6 +55,12 @@ int partition(int arr[], int low, int high) { return (i + 1); } +/** + * The main function that implements QuickSort + * arr[] --> Array to be sorted, + * low --> Starting index, + * high --> Ending index +*/ void quickSort(int arr[], int low, int high) { if (low < high) { int p = partition(arr, low, high); @@ -37,6 +69,7 @@ void quickSort(int arr[], int low, int high) { } } +// prints the array after sorting void show(int arr[], int size) { for (int i = 0; i < size; i++) std::cout << arr[i] << " "; @@ -44,7 +77,6 @@ void show(int arr[], int size) { } // Driver program to test above functions - int main() { int size; std::cout << "\nEnter the number of elements : "; From 5a54093484588b2a11e96b40dca22e3a0ee42e8a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 27 May 2020 13:24:42 +0000 Subject: [PATCH 5/5] updating DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 636543a25..9fdf260a9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -196,7 +196,6 @@ * [Non Recursive Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/non_recursive_merge_sort.cpp) * [Numericstringsort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/NumericStringSort.cpp) * [Oddeven Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/OddEven%20Sort.cpp) - * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Quick%20Sort.cpp) * [Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort.cpp) * [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp) * [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp)