From f84976ab8369b350b76801d680ae371166c855c0 Mon Sep 17 00:00:00 2001 From: Satyam Singh <56315878+satcasm@users.noreply.github.com> Date: Sun, 31 Jan 2021 18:20:51 +0530 Subject: [PATCH] added quick sort (#48) * add binary search in js * added merge sort in c * added quick sort in cpp * modified quick sort --- sorting/README.md | 1 + sorting/c-or-cpp/merge-sort.c | 13 +++-- sorting/c-or-cpp/quick-sort.cpp | 77 +++++++++++++++++++++++++++++ sorting/c-or-cpp/selection-sort.cpp | 2 +- 4 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 sorting/c-or-cpp/quick-sort.cpp diff --git a/sorting/README.md b/sorting/README.md index 84457784..b00bbae7 100644 --- a/sorting/README.md +++ b/sorting/README.md @@ -6,6 +6,7 @@ 2. [Insertion Sort](c-or-cpp/insertion-sort.cpp) 3. [Selection Sort](c-or-cpp/selection-sort.cpp) 4. [Merge Sort](c-or-cpp/merge-sort.c) +5. [Quick Sort](c-or-cpp/quick-sort.cpp) ### C# diff --git a/sorting/c-or-cpp/merge-sort.c b/sorting/c-or-cpp/merge-sort.c index cdc64ae8..3d66da6e 100644 --- a/sorting/c-or-cpp/merge-sort.c +++ b/sorting/c-or-cpp/merge-sort.c @@ -13,11 +13,11 @@ int main() printf("Enter the elements of the array:\n"); for (int i = 0; i < n; i++) scanf("%d", &a[i]); - printf( "Given array is: "); + printf("Given array is: "); for (int i = 0; i < n; i++) printf("%d ", a[i]); mergeSort(a, 0, n - 1); - printf( "\nSorted array is: "); + printf("\nSorted array is: "); for (int i = 0; i < n; i++) printf("%d ", a[i]); return 0; @@ -30,7 +30,7 @@ void merging(int a[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; - int Left[n1], Right[n2]; // Creating temporary arrays + int Left[n1], Right[n2]; // Creating temporary arrays // Copy data to temp arrays Left[] and Right[] for (int i = 0; i < n1; i++) @@ -55,7 +55,7 @@ void merging(int a[], int l, int m, int r) } // Copy the remaining elements of - // Left[], if there are any + // Left[], if there are any while (p < n1) { a[k] = Left[p]; @@ -64,7 +64,7 @@ void merging(int a[], int l, int m, int r) } // Copy the remaining elements of - // Right[], if there are any + // Right[], if there are any while (q < n2) { a[k] = Right[q]; @@ -73,10 +73,9 @@ void merging(int a[], int l, int m, int r) } } - // l is for left index and r is // right index of the sub-array -// of arr to be sorted +// of arr to be sorted void mergeSort(int a[], int l, int r) { if (l < r) diff --git a/sorting/c-or-cpp/quick-sort.cpp b/sorting/c-or-cpp/quick-sort.cpp new file mode 100644 index 00000000..d20a357a --- /dev/null +++ b/sorting/c-or-cpp/quick-sort.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; +int partition(int a[],int low,int high); +void quickSort(int a[], int low, int high); + +// Driver Code +int main() +{ + cout<<"Enter the length of array"<>n; + int *a = new int(n); + // Getting elements of array + cout<<"Enter the elements of array"<>a[i]; + cout << "Original array: \n"; + for (int i = 0; i < n; i++) + cout << a[i] << " "; + quickSort(a, 0, n - 1); + cout << "\nSorted array: \n"; + for (int i = 0; i < n; i++) + cout << a[i] << " "; + delete (a); + return 0; +} + +// A utility function to swap two elements +void swap(int *a, int *b) +{ + int t = *a; + *a = *b; + *b = t; +} + +/* This function takes last element as pivot, places +the pivot element at its correct position in sorted +aay, and places all smaller (smaller than pivot) +to left of pivot and all greater elements to right +of pivot */ +int partition(int a[], int low, int high) +{ + int pivot = a[high]; + int i = (low - 1); + + for (int j = low; j <= high - 1; j++) + { + // If current element is smaller than the pivot + if (a[j] < pivot) + { + i++; // increment index of smaller element + swap(&a[i], &a[j]); + } + } + swap(&a[i + 1], &a[high]); + return (i + 1); +} + +/* The main function that implements QuickSort +a[] --> array to be sorted, +low --> Starting index, +high --> Ending index */ +void quickSort(int a[], int low, int high) +{ + if (low < high) + { + /* pi is partitioning index, a[p] is now + at right place */ + int pi = partition(a, low, high); + + // Separately sort elements before + // partition and after partition + quickSort(a, low, pi - 1); + quickSort(a, pi + 1, high); + } +} + diff --git a/sorting/c-or-cpp/selection-sort.cpp b/sorting/c-or-cpp/selection-sort.cpp index 629f0eb2..e8c3f698 100644 --- a/sorting/c-or-cpp/selection-sort.cpp +++ b/sorting/c-or-cpp/selection-sort.cpp @@ -1,4 +1,4 @@ -# include +#include using namespace std; int main(int argc, char const *argv[])