forked from cg-humanore/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuicksort.c
More file actions
47 lines (47 loc) · 661 Bytes
/
Quicksort.c
File metadata and controls
47 lines (47 loc) · 661 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
#include<stdio.h>
int partition(int A[],int p,int r)
{
int x,i,j,t,s;
x=A[r];
i=p-1;
for(j=p;j<r;j++)
{
if(A[j]<=x)
{
i=i+1;
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
s=A[i+1];
A[i+1]=A[r];
A[r]=s;
return i+1;
}
void Quicksort(int A[],int p,int r)
{
int q;
if(p<r)
{
q=partition(A,p,r);
Quicksort(A,p,q-1);
Quicksort(A,q+1,r);
}
}
int main()
{
int A[30],i,n;
printf("Enter Size:");
scanf("%d",&n);
printf("Enter Elements:");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\n Before Sort :");
for(i=0;i<n;i++)
printf("\n %d",A[i]);
Quicksort(A,0,n-1);
printf("\nAfter sort");
for(i=0;i<n;i++)
printf("\n %d",A[i]);
}