forked from CodXCrypt/Cpp-Programs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicksort.cpp
50 lines (48 loc) · 834 Bytes
/
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
#include<bits/stdc++.h>
using namespace std;
int partition(int a[],int lb,int ub)
{
int pivot=a[lb];
int start=lb;
int end=ub;
while(start<end)
{
while(a[start]<=pivot)
{
start++;
}
while(a[end]>pivot)
{
end--;
}
if(start<end)
swap(a[start],a[end]);
}
swap(a[lb],a[end]);
return end;
}
void quicksort(int a[],int lb,int ub)
{
if(lb<ub)
{
int loc;
loc=partition(a,lb,ub);
quicksort(a,lb,loc-1);
quicksort(a,loc+1,ub);
}
}
void print(int a[], int n)
{
int i;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
int main()
{
int a[]={7,6,10,5,9,2,1,15,7};
int n=sizeof(a)/sizeof(a[0]);
cout<<"After quicksort"<<endl;
quicksort(a,0,n-1);
print(a,n);
return 0;
}