-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick sort.c
66 lines (54 loc) · 888 Bytes
/
quick sort.c
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
#include<stdio.h>
#include<conio.h>
main()
{
int a[20]={9,8,7,6,5,4,3,2,1};
int i;
printf("the un sorted list will be:");
for(i=0;i<9;i++)
{
printf("%d",a[i]);
}
sort(0,8);
printf("The sorted algorith will be");
for(i=0;i<9;i++)
{
printf("%d",a[i]);
}
getch();
}
int sort(int low,int high)
{
int pivotlocation;
if(low<high)
{
pivotlocation=divide(low,high);
sort(low,pivotlocation);
sort(pivotlocation+1,high);
}
}
int divide(int low,int high)
{
int a[20]={9,8,7,6,5,4,3,2,1};
int mid,pivot,i,lastsmall,t;
mid=low+high/2;
pivot=a[mid];
t=a[mid];
a[mid]=a[low];
a[low]=t;
lastsmall=low;
for(i=low+1;i<high;i++)
{
if(a[i]<pivot)
{
lastsmall++;
t=a[i];
a[i]=a[lastsmall];
a[lastsmall]=t;
}
}
t=a[low];
a[low]=a[lastsmall];
a[lastsmall]=t;
return lastsmall;
}