-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.cpp
56 lines (42 loc) · 1010 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
#include<iostream>
using namespace std;
//pick last element as pivot element and place it in its correct place and
//place all smaller elements to left and larger elements to the right of the pivot
int partition_qs(int arr[],int l, int h){
int pivot=arr[l];
int i=l,j=h;
while(i<j)
{
do
{
i++;
}while(arr[i]<=pivot);
do
{
j--;
}while(arr[j]>pivot);
if(i<j)swap(arr[i],arr[j]);
}
swap(arr[l],arr[j]);
return j;
}
void quicksort(int arr[],int l, int h)
{
int p;
if(l<h) // atleast there should be two elements
{
p=partition_qs(arr,l,h);
quicksort(arr,l,p);
quicksort(arr, p+1, h);
}
}
int main()
{
int arr[]={11,13,7,12,16,9,24,5,10,3,INT_MAX};
quicksort(arr,0,10);
cout<<"Sorted Array: "<<endl;
for(int i=0;i<10;i++){
cout<<arr[i]<<"\t";
}
return 0;
}