-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick sort.c
58 lines (52 loc) · 960 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
#include<stdio.h>
void swap(int *a,int x,int y)
{
int temp;
temp=a[x];
a[x]=a[y];
a[y]=temp;
}
void quicksort(int *a,int first,int last)
{
int k;
int pivot,i,j;
if(first<last)
{
i=first;
j=last;
pivot=(i+j)/2;
printf("pivot%d\n",a[pivot]);
printf("\nj=%d\n",j);
while(i<j)
{
while(a[i]<=a[pivot]&& i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
swap(a,i,j);
}
for(k=0;k<7;k++)
printf("%d ",a[k]);
printf("\n");
printf("\nj=%d\n",j);
}
swap(a,j,pivot);
for(k=0;k<7;k++)
printf("%d ",a[k]);
printf("\nj=%d\n",j);
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
void main()
{
int i,a[]={5,8,4,6,4,3,1};
for(i=0;i<7;i++)
printf("%d ",a[i]);
printf("\n");
quicksort(a,0,6);
for(i=0;i<7;i++)
printf("%d ",a[i]);
}