-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.c
59 lines (59 loc) · 1.06 KB
/
quick_sort.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
#include<stdio.h>
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void quicksort(int a[],int s,int e)
{
if(s<e)
{
int p=a[s];
int i=s+1;
int j=e;
while(i<=j)
{
while(i<=e && a[i]<p)
{
i++;
}
while(j>s && a[j]>p)
{
j--;
}
if(i<j)
{
swap(&a[i],&a[j]);
i++;
j--;
}
}
swap(&a[s],&a[j]);
quicksort(a,s,j-1);
quicksort(a,j+1,e);
}
}
void main()
{
int i,n;
printf("Read the array size : ");
scanf("%d",&n);
int a[n];
printf("Enter the elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("befor sorting : ");
for(i=0;i<n;i++)
{
printf("%d " ,a[i]);
}
quicksort(a,0,n-1);
printf("After sorting : ");
for(i=0;i<n;i++)
{
printf("%d " ,a[i]);
}
}