-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick_Sort.c
54 lines (51 loc) · 1.29 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
#include<stdio.h>
int partition(int array[], int lb, int ub) {
int pivot, start, end, temp;
pivot = array[lb];
start = lb;
end = ub;
while(start < end) {
while(array[start] <= pivot)
start++;
while(array[end] > pivot)
end--;
if(start < end) {
temp = array[start];
array[start] = array[end];
array[end] = temp;
}
}
temp = array[lb];
array[lb] = array[end];
array[end] = temp;
return end;
}
void quickSort(int array[], int lb, int ub) {
if(lb < ub) {
int loc = partition(array, lb, ub);
quickSort(array, lb, loc-1);
quickSort(array, loc+1, ub);
}
}
int main() {
int length, i, lb = 0, ub;
int array[100];
//clrscr();
printf("\nEnter the length of the array : ");
scanf("%d", &length);
printf("\nEnter the array : ");
for(i = 0 ; i < length ; i++) {
scanf("%d", &array[i]);
}
printf("\n\nUNSORTED ARRAY : ");
for(i = 0 ; i < length ; i++) {
printf("%d ", array[i]);
}
ub = length - 1;
quickSort(array, lb, ub);
printf("\nSORTED ARRAY : ");
for(i = 0 ; i < length ; i++) {
printf("%d ", array[i]);
}
return 0;
}