-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
59 lines (53 loc) · 807 Bytes
/
Copy pathQuickSort.cpp
File metadata and controls
59 lines (53 loc) · 807 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
55
56
57
58
59
#include<iostream>
#include <algorithm>
using namespace std;
int A[8];
int partition(int l,int h)
{
int pivot=A[l];
int i=l-1;
int j=h+1;
while(true)
{
do
{
i++;
} while(A[i] < pivot);
do
{
j--;
} while(A[j] > pivot);
if(i >= j)
return j;
swap(A[i], A[j]);
}
}
void quickSort(int l,int h)
{
if(l<h)
{
int pi=partition(l,h);
quickSort(l,pi);
quickSort(pi+1,h);
}
}
int main()
{
cout<<"Enter elements of your array:";
for(int i=0;i<8;i++)
{
cin>>A[i];
}
cout<<"Array before swap:";
for(int i=0;i<8;i++)
{
cout<<A[i]<<"\t";
}
quickSort(0,7);
cout<<"\n Sorted array:";
for(int i=0;i<8;i++)
{
cout<<A[i]<<"\t";
}
return 0;
}