-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathQuickSort.cpp
58 lines (49 loc) · 1.06 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
#include<iostream>
using namespace std;
int partition(int* arr, int s, int e){
int temp = arr[s];
int count = 0;
for (size_t x = s+1; x <= e; x++)
{
if(arr[x] <= temp) count++;
}
int pivot = s + count;
// cout<< arr[pivot] << " ";
swap(arr[pivot],arr[s]);
int i = s, j = e;
while(i < pivot && j > pivot){
while(arr[i] <= arr[pivot])
{
i++;
}
while(arr[j] > arr[pivot]) {
j--;
}
if(i < pivot && j > pivot){
swap(arr[i++], arr[j--]);
}
}
return pivot;
}
void QuickSort(int* arr, int s, int e){
if(s>=e) return;
int p = partition(arr,s,e);
// cout << endl;
QuickSort(arr,s,p-1);
QuickSort(arr,p+1,e);
}
int main(int argc, char const *argv[])
{
int arr[] = {1,5,3,8,6,9,4};
for (size_t i = 0; i < 7; i++)
{
cout << arr[i] << " ";
}
cout << endl << endl;
QuickSort(arr, 0, 6);
for (size_t i = 0; i < 7; i++)
{
cout << arr[i] << " ";
}
return 0;
}