-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.c
More file actions
56 lines (46 loc) · 1.03 KB
/
Copy pathQuickSort.c
File metadata and controls
56 lines (46 loc) · 1.03 KB
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
#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 = a[lb];
a[lb] = a[end];
a[end] = temp;
return end;
}
void QuickSort(int a[], int lb, int ub) {
if (lb < ub) {
int pivotIndex = Partition(a, lb, ub);
QuickSort(a, lb, pivotIndex - 1);
QuickSort(a, pivotIndex + 1, ub);
}
}
int main() {
int a[100];
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &a[i]);
}
QuickSort(a, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}