From 13990d15cda3baec5a65225135f57c483a9ea44b Mon Sep 17 00:00:00 2001 From: vishad2 <31646315+vishad2@users.noreply.github.com> Date: Sat, 6 Oct 2018 17:14:58 +0530 Subject: [PATCH] Update QuickSort.cpp Quicksort implementation without using class --- Sorting/Quick Sort/C++/QuickSort.cpp | 99 ++++++++-------------------- 1 file changed, 29 insertions(+), 70 deletions(-) diff --git a/Sorting/Quick Sort/C++/QuickSort.cpp b/Sorting/Quick Sort/C++/QuickSort.cpp index 959c7705..20d76cad 100644 --- a/Sorting/Quick Sort/C++/QuickSort.cpp +++ b/Sorting/Quick Sort/C++/QuickSort.cpp @@ -4,78 +4,37 @@ using namespace std; -class QuickSort +partition (arr[], low, high) { - /* 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 */ - public: - - int partition(int arr[], int low, int high) - { - int pivot = arr[high]; - int i = (low-1); // index of smaller element - for (int j=low; j Array to be sorted, - low --> Starting index, - high --> Ending index */ - void sort(int arr[], int low, int high) - { - if (low < high) - { - /* pi is partitioning index, arr[pi] is - now at right place */ - int pi = partition(arr, low, high); - - // Recursively sort elements before - // partition and after partition - sort(arr, low, pi-1); - sort(arr, pi+1, high); - } - } -}; - - // Driver program -int main() + // pivot (Element to be placed at right position) + pivot = arr[high]; + + i = (low - 1) // Index of smaller element + + for (j = low; j <= high- 1; j++) + { + // If current element is smaller than or + // equal to pivot + if (arr[j] <= pivot) + { + i++; // increment index of smaller element + swap arr[i] and arr[j] + } + } + swap arr[i + 1] and arr[high]) + return (i + 1) +} +quickSort(arr[], low, high) { - int arr[] = {10, 7, 8, 9, 1, 5}; - int n = *(&arr + 1) - arr; - - QuickSort ob; - ob.sort(arr, 0, n-1); - - cout<<"sorted array: "; - for (int i=0; i