-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.c
58 lines (48 loc) · 835 Bytes
/
QuickSort.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>
#define SIZE 6
int A[SIZE] = {10, 9, 8, 7, 1, 5}; // = {6, 9, 4, 15, 5, 7};
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int
getPos(int a[], int low, int high)
{
int pivot = a[high];
int i = (low -1);
for (int p = low; p <= high - 1; p++)
{
if (a[p] <= pivot) {
i++;
swap(&a[i], &a[p]);
}
}
swap(&a[i + 1], &a[high]);
return i + 1;
}
void
QuickSort(int a[], int low, int high)
{
if (low < high)
{
int pi = getPos(a, low, high);
QuickSort(a, low, pi - 1);
QuickSort(a, pi + 1, high);
}
}
int
printArray (int a[], int n)
{
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
int main(int argc, char const *argv[]) {
printArray(A, SIZE);
QuickSort(A, 0, SIZE-1);
printArray(A, SIZE);
return 0;
}