forked from baharodia-devaj/c-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick sort
More file actions
58 lines (57 loc) · 796 Bytes
/
quick sort
File metadata and controls
58 lines (57 loc) · 796 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
53
54
55
56
57
58
#include<stdio.h>
#define MAX 100
int partition(int arr[],int lb,int up)
{
int temp,i,j,pivot;
i=lb+1;
j=up;
pivot=arr[lb];
while(i<=j)
{
while((arr[i]<pivot) && (i<up))
i++;
while(arr[j]>pivot)
j--;
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
else
i++;
}
arr[lb]=arr[j];
arr[j]=pivot;
return j;
}
void quick(int sort[],int lb,int up)
{
int p;
if(lb<up)
{
p=partition(sort,lb,up);
quick(sort,lb,p-1);
quick(sort,p+1,up);
}
}
int main()
{
int sort[MAX],n,i,j;
printf("\n enter the number of element of array ");
scanf("%d",&n);
printf("\n enter elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&sort[i]);
}
quick(sort,0,n-1);
printf("sorted list is:\n");
for(i=0;i<n;i++)
{
printf("%d ",sort[i]);
}
return 0;
}