-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick_sort.c
51 lines (50 loc) · 858 Bytes
/
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
#define MAX 100
#include<stdio.h>
void quicksort(int [],int,int);
int main()
{
int n,t=0;
printf("Enter the size of array : ");
scanf("%d",&n);
int A[n];
printf("Enter the elements of the array: ");
for(int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
quicksort(A,t,n-1);
printf("The sorted array is: ");
for(int i=0;i<n;i++)
{
printf("%d ",A[i]);
}
}
void quicksort(int A[],int lb,int ub)
{
int i=lb,j=ub,key=A[lb],t=0;
if(lb>=ub)
{
return;
}
while(i<=j)
{
while(key>=A[i] &&i<j)
{
i++;
}
while(key<A[j])
{
j--;
}
if(i<j)
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
A[lb]=A[j];
A[j]=key;
quicksort(A,lb,j-1);
quicksort(A,j+1,ub);
}