-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick.c
executable file
·62 lines (55 loc) · 1016 Bytes
/
quick.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
62
/* Quick sort is one of the most efficient sorting algorithm which has complexity order of nlogn in its best case and n*n in its worst case*/
#include<stdio.h>
int partition(int a[],int i,int j)
{
int l=i;
int r=j;
int pivot=a[i],temp;
while(l<r)
{
while(a[l]<=pivot)
{l++;}
while(a[r]>pivot)
{r--;}
if(l<r)
{ temp=a[l]; /* swap greater value(left) with smaller value(right) */
a[l]=a[r];
a[r]=temp;
}
}
a[i]=a[r]; /*swap pivot value with samller(a[r]) or the same(itself) value*/
a[r]=pivot;
return r;
}
void quicksort(int a[],int l,int h)
{
if(l<h)
{
int p=partition(a,l,h);
quicksort(a,l,p-1);
quicksort(a,p+1,h);
}
}
int main(int argc, char* argv[])
{
int val,i=0,j;
int a[101];
FILE *input, *output;
input=fopen(argv[1],"r=");
output=fopen(argv[2],"w+");
while(fscanf(input,"%d\t",&val)!=EOF)
{
a[i++]=val;
}
quicksort(a,0,i-1);
printf("\nSorted array is : \n");
for(j=0;j<i;j++)
{
printf("%d\t",a[j]);
fprintf(output,"%d\t",a[j]);
}
printf("\n");
fclose(input);
fclose(output);
return 0;
}