-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathquickSort.c
60 lines (47 loc) · 1.16 KB
/
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
59
60
#include <stdio.h>
void swap(int array[] ,int index1, int index2);
int partition(int array[], int low, int high);
void quickSort(int array[], int low, int high);
int main() {
int array[9] = {2,9,3,8,6,5,4,7,10};
printf("Original array: ");
for (int i = 0; i < 9; i++) {
printf("%d ", array[i]);
}
quickSort(array, 0,9);
printf("\nSorted array: ");
for (int i = 0; i < 9; i++) {
printf("%d ", array[i]);
}
return 0;
}
void swap(int array[], int index1, int index2) {
int temp = array[index2];
array[index2] = array[index1];
array[index1] = temp;
}
int partition(int array[], int low, int high) {
int pivot = array[low];
int i = 0;
int j = high;
while(i < j) {
do {
i++;
} while (array[i] <= pivot);
do {
j--;
} while (array[j] > pivot);
if (i < j) {
swap(array, i, j);
}
}
swap(array, low, j);
return j;
}
void quickSort(int array[], int low, int high) {
if (low < high) {
int j = partition(array, low, high);
quickSort(array, low, j);
quickSort(array, j+1, high);
}
}