-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHOARE.CPP
70 lines (60 loc) · 927 Bytes
/
HOARE.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
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <conio.h>
#include <iostream.h>
#define MAX 32767
int partition(int,int);
void sort(int,int);
int a[10]={0};
int partition(int low,int high)
{
int i=low+1,j=high;
int key=a[low],temp;
while(1)
{
if((i<high) && (key>=a[i]))
i++;
if(key<a[j])
j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}
void sort(int n,int k)
{
int low=0,high=n;
a[n]=MAX;
while(1)
{
int j=partition(low,high);
if(k==j) return;
else
if(k<j) high=j;
else low=j+1;
}
}
void main()
{
clrscr();
int n;
cout<<"\nEnter the size:";
cin>>n;
cout<<"\nEnter the elements to be sorted:";
for(int i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
sort(n,i);
cout<<"\nSorted array is :";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}