-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.c
52 lines (43 loc) · 981 Bytes
/
quicksort.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
#include<stdio.h>
void quicksort(int[100],int,int);
void main()
{
int x[100],i,size;
printf("Enter size\n");
scanf("%d",&size);
printf("\nEnter elements to be sorted\n");
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("\nSorted array\n");
for(i=0;i<size;i++)
printf("%d ",x[i]);
}
void quicksort(int x[100],int first,int last)
{
int i,j,pivot,temp;
if(first<last)
{
i=first;
pivot=first;
j=last;
while(i<j)
{
while(x[i]<x[pivot] && i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}