-
Notifications
You must be signed in to change notification settings - Fork 0
/
Qsort.c
84 lines (65 loc) · 1.26 KB
/
Qsort.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
int partition (int a[],int lb,int ub){
int pivot = a[lb];
int start = lb;
int end = ub;
while(start<end){
while(a[start] <= pivot){
start++;
}
while (a[end]>pivot){
end--;
}
if(start<end){
int temp=a[start];
a[start]=a[end];
a[end]=temp;
}
}
int temp=pivot;
a[lb]=a[end];
a[end]=pivot;
return end;
}
void quicksort(int a[],int lb,int ub){
int loc;
if(lb<ub){
loc=partition(a, lb, ub);
quicksort(a,lb,loc-1);
quicksort(a,loc+1,ub);
}
}
int main(){
int size;
printf("Enter the Size:");
scanf("%d",&size);
int a[size] ;
printf("Enter %d Elements:\n",size);
for(int I=0; I<size;I++){
scanf("%d",&a[I]);
}
printf("Array Elements are:");
for(int I=0; I<size;I++){
printf("%3d",a[I]);
}
int lb=0;
int ub=size-1;
quicksort(a,lb, ub);
printf("\nSorted Array Elements:");
for(int I =0;I<size;I++){
printf("%3d",a[I]);
}
return 0;
}
-----------------------
output:
Enter the Size:6
Enter 6 Elements:
2
4
1
3
2
5
Array Elements are: 2 4 1 3 2 5
Sorted Array Elements: 1 2 2 3 4 5