forked from krishnamanojpvr/DSCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.cpp
57 lines (54 loc) · 1.02 KB
/
Quick_Sort.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
#include <iostream>
using namespace std;
int partition(int arr[], int lb, int ub)
{
int pivot = arr[lb];
int start = lb;
int end = ub;
while (start < end)
{
while (arr[start] <= pivot)
{
start++;
}
while (arr[end] > pivot)
{
end--;
}
if (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
int temp = arr[lb];
arr[lb] = arr[end];
arr[end] = temp;
return end;
}
void quickSort(int arr[], int lb, int ub)
{
if (lb < ub)
{
int loc = partition(arr, lb, ub);
quickSort(arr, lb, loc - 1);
quickSort(arr, loc + 1, ub);
}
}
int main()
{
int arr[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cout << endl;
quickSort(arr, 0, 9);
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}