-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.cpp
68 lines (58 loc) · 1.79 KB
/
QuickSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<vector>
using namespace std;
int partition(int arr[], int s, int e) {
// Step 1: Select the pivot element
int pivotIndex = s;
int pivot = arr[s];
// Step 2: Find the correct place of the pivot element
int count = 0;
for (int i = s + 1; i <= e; i++) {
if (arr[i] <= pivot)
count++;
}
// Place the pivot in its correct position
int rightIndex = s + count;
swap(arr[pivotIndex], arr[rightIndex]);
pivotIndex = rightIndex;
// Step 3: Create the partition so that left elements are smaller than pivot and right elements are greater than pivot
int i = s;
int j = e;
while (i < pivotIndex && j > pivotIndex) {
// Move i to the right until it points to an element greater than pivot
while (arr[i] <= pivot)
i++;
// Move j to the left until it points to an element smaller or equal to pivot
while (arr[j] > pivot)
j--;
// Swap elements if i is behind pivotIndex and j is ahead of pivotIndex
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i], arr[j]);
i++;
j--;
}
}
return pivotIndex; // Return the index of the pivot element after partitioning
}
void quickSort(int arr[],int s,int e){
// Base case
if(s >= e) // s == e is single element which is already sorted and s > e is invalid array
return;
// Partition Logic
int p = partition(arr,s,e);
// Recursion Logic
// Call for left array
quickSort(arr,s,p-1);
// Call for right array
quickSort(arr,p+1,e);
}
int main(){
int arr[] = {38,27,43,3,9,20};
int n = 6;
int s = 0;
int e = n - 1;
quickSort(arr,s,e);
for(int i = 0; i<n;i++)
cout<<arr[i]<<" ";
return 0;
}