-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
53 lines (48 loc) · 760 Bytes
/
quick_sort.cpp
File metadata and controls
53 lines (48 loc) · 760 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
#include<iostream>
using namespace std;
int Partition (int *a,int p,int r)
{
int x=a[r],q;
int i,j,temp;
i=p-1;
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
temp=a[i+1];
a[i+1]=a[j];
a[j]=temp;
i++;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
q=i+1;
return q;
}
void Quicksort(int *a,int p,int r)
{
if(p<r)
{
int q;
q=Partition(a,p,r);
Quicksort(a,p,q-1);
Quicksort(a,q+1,r);
}
}
int main()
{
int a[8]={2,6,7,1,3,5,6,4};
for(int i=0;i<8;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
Quicksort(a,0,7);
for(int i=0;i<8;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}