-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
49 lines (44 loc) · 781 Bytes
/
QuickSort.cpp
File metadata and controls
49 lines (44 loc) · 781 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
/*
Quick sort Algorithm
Partition using Hoare's partition scheme*/
#include<stdio.h>
int partition(int ar[], int l, int h)
{
int pivot=ar[l];
int i=l-1;
int j=h+1;
while(true){
do{
i++;
}while(ar[i]<pivot);
do{
j--;
}while(ar[j]>pivot);
if(i>=j){
return j;
}
//swap
ar[i]=ar[i]+ar[j];
ar[j]=ar[i]-ar[j];
ar[i]=ar[i]-ar[j];
}
}
void QuickSort(int ar[],int l,int h)
{
if(l<h)
{
int pivot=partition(ar,l,h);
QuickSort(ar,l,pivot);
QuickSort(ar,pivot+1,h);
}
}
int main()
{
int ar[]={12,2,8,-24,0,1};
QuickSort(ar,0,5);
for(int i=0;i<6;i++)
{
printf("%d ",ar[i]);
}
return 0;
}