Skip to content

Commit 3320b9f

Browse files
authored
Merge pull request #27 from neha030/devp1
quick sort added
2 parents f9e2384 + 14f101b commit 3320b9f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

quicksort.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int partition(int a[],int lb,int ub)
4+
{
5+
int pivot=a[lb];
6+
int start=lb;
7+
int end=ub;
8+
while(start<end)
9+
{
10+
while(a[start]<=pivot)
11+
{
12+
start++;
13+
}
14+
while(a[end]>pivot)
15+
{
16+
end--;
17+
}
18+
if(start<end)
19+
swap(a[start],a[end]);
20+
21+
}
22+
swap(a[lb],a[end]);
23+
return end;
24+
}
25+
void quicksort(int a[],int lb,int ub)
26+
{
27+
if(lb<ub)
28+
{
29+
int loc;
30+
loc=partition(a,lb,ub);
31+
quicksort(a,lb,loc-1);
32+
quicksort(a,loc+1,ub);
33+
34+
}
35+
}
36+
void print(int a[], int n)
37+
{
38+
int i;
39+
for(i=0;i<n;i++)
40+
cout<<a[i]<<" ";
41+
}
42+
int main()
43+
{
44+
int a[]={7,6,10,5,9,2,1,15,7};
45+
int n=sizeof(a)/sizeof(a[0]);
46+
cout<<"After quicksort"<<endl;
47+
quicksort(a,0,n-1);
48+
print(a,n);
49+
return 0;
50+
}

0 commit comments

Comments
 (0)