-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_Sort.cpp
More file actions
54 lines (46 loc) · 818 Bytes
/
Quick_Sort.cpp
File metadata and controls
54 lines (46 loc) · 818 Bytes
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
// QuickSort code
#include <bits/stdc++.h>
using namespace std;
int ar[6] = {8, 4, 6, 3, 2, 9};
int part(int l, int h)
{
int pivot = ar[l];
int i = l, j = h;
while (i < j)
{
while (ar[i] <= pivot)
{
i++;
}
while (ar[j] > pivot)
{
j--;
}
if (i < j)
{
swap(ar[i], ar[j]);
}
}
swap(ar[j], ar[l]);
return j;
}
void quickSort(int l, int h)
{
if (l < h)
{
int j = part(l, h); // partition
quickSort(l, j);
quickSort(j + 1, h);
}
}
int main()
{
ar[6] = INT_MAX;
int l = 0;
int h = 6;
quickSort(l, h);
for (int i = 0; i < 6; i++)
{
cout << ar[i] << ' ';
}
}