forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort.cpp
57 lines (56 loc) · 795 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
51
52
53
54
55
56
57
#include<bits/stdc++.h>
using namespace std;
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int partition(int arr[],int lb,int ub)
{
int pivot=arr[lb];
int start=lb;
int end=ub;
while(start<end)
{
while(arr[start]<=pivot)
{
start++;
}
while(arr[end]>pivot)
{
end--;
}
if(start<end)
{
swap(&arr[start],&arr[end]);
}
}
swap(&arr[lb],arr[end]);
return end;
}
void quicksort(int a[],int lb,int ub)
{
if(lb<ub)
{
int loc=partition(a,lb,ub);
quicksort(a,lb,loc-1);
quicksort(a,lb+1,ub);
}
}
void print(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int arr[]={7,6,10,5,9,2,1,15,7};
int n=sizeof(arr)/sizeof(arr[0]);
quicksort(arr,0,n-1);
print(arr,n);
return 0;
}