-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.c
55 lines (47 loc) · 1.17 KB
/
5.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
#include <stdio.h>
#include <stdlib.h>
int a[20], i, j, l, u, n, p, loc;
void quicksort(int a[], int lb, int ub);
int partition(int a[], int lb, int ub);
void main() {
printf("enter the number of elements : \n");
scanf("%d", &n);
printf("enter the %d elements : \n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
quicksort(a, 0, n - 1);
for (i = 0; i < n; i++) {
printf("%d\t", a[i]);
}
}
void quicksort(int a[], int lb, int ub) {
if (lb < ub) {
loc = partition(a, lb, ub);
quicksort(a, lb, loc - 1);
quicksort(a, loc + 1, ub);
}
}
int partition(int a[], int lb, int ub) {
p = lb;
int start = lb + 1; // Start with the element next to pivot
int end = ub;
while (start <= end) {
while (a[start] <= a[p] && start <= ub) {
start++;
}
while (a[end] > a[p]) {
end--;
}
if (start < end) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
}
// Swap pivot with element at 'end' position
int temp = a[p];
a[p] = a[end];
a[end] = temp;
return end;
}