-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuick_Sort.c
More file actions
57 lines (57 loc) · 1.08 KB
/
Quick_Sort.c
File metadata and controls
57 lines (57 loc) · 1.08 KB
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
#include<stdio.h>
int partition(int a[],int lb,int ub)
{
int start,end,pivot,t1,t2;
pivot=a[lb];
start=lb;
end=ub;
while(start<end)
{
while(a[start]<=pivot)
{
start++;
}
while(a[end]>pivot)
{
end--;
}
if(start<end)
{
t1=a[start];
a[start]=a[end];
a[end]=t1;
}
}
t2=a[lb];
a[lb]=a[end];
a[end]=t2;
return end;
}
void QuickSort(int a[],int lb,int ub)
{
if(lb<ub)
{
int loc;
loc=partition(a,lb,ub);
QuickSort(a,lb,loc-1);
QuickSort(a,loc+1,ub);
}
}
int main()
{
int i,n,a[100];
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements separated by space : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The unsorted elements are : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
QuickSort(a,0,n-1);
printf("The sorted elements are : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}