-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_sort.c
More file actions
52 lines (52 loc) · 792 Bytes
/
quick_sort.c
File metadata and controls
52 lines (52 loc) · 792 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
50
51
52
#include<conio.h>
#include<stdio.h>
#define size 7
void quick(int *,int,int,int);
void swap(int *,int *);
void main()
{
int i,a[size],f;
char g;
printf("enter elements..");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\nA-ascending\nD-descending\n");
get:g=getch();
if(g=='a'||g=='A')
f=1;
else if(g=='d'||g=='D')
f=-1;
else
goto get;
quick(&a[0],0,size-1,f);
printf("\nsorted list:");
for(i=0;i<size;i++)
printf("%d ",a[i]);
}
void quick(int *a,int l,int u,int f)
{
int f1=1,i,j,p,k;
p=l;
i=l+1;
j=u;
if(l>=u)
return;
for(;f1;)
{
for(;(*(a+i))*f<(*(a+p))*f;i++);
for(;(*(a+j))*f>(*(a+p))*f;j--);
if(i<j)
swap(a+i,a+j);
else f1=0;
}
swap(a+l,a+j);
quick(a,l,j-1,f);
quick(a,j+1,u,f);
}
void swap(int *i,int *j)
{
int t;
t=*i;
*i=*j;
*j=t;
}