forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_sort.c
58 lines (48 loc) · 968 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
52
53
54
55
56
57
58
#include <stdio.h>
int partition(int x[],int lb,int ub){
int up,down,pivot,temp;
pivot=x[lb];
up=ub;
down=lb;
while(down<up)
{
while(x[down]<=pivot && down<ub){
down++;
}
while(x[up]>pivot){
up--;
}
if(down<up)
{
temp=x[down];
x[down]=x[up];
x[up]=temp;
}
}
x[lb]=x[up];
x[up]=pivot;
return up;
}
void quicksort(int arr[], int lb, int ub){
int j;
if (lb>=ub)
return;
j = partition(arr,lb,ub);
quicksort(arr,lb,j-1);
quicksort(arr,j+1,ub);
}
int main()
{
int n,i,v,arr[100];
printf("enter the total no of elements: ");
scanf("%d",&n);
printf("enter the values: \n");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
quicksort(arr,0,n-1);
printf("Order of Sorted elements using quick sort:\n ");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
return 0;
}