-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.c
More file actions
67 lines (55 loc) · 1.08 KB
/
Copy pathquick_sort.c
File metadata and controls
67 lines (55 loc) · 1.08 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
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
void displayArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int partition(int arr[], int low, int high)
{
int pivot = arr[low];
int i = low + 1;
int j = high;
int temp;
do
{
while (arr[i] <= pivot)
{
i++;
}
while (arr[j] > pivot)
{
j--;
}
if (i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
} while (i < j);
temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
return j;
}
void quick_sort(int arr[], int low, int high)
{
int parIdx;
if (low < high)
{
parIdx = partition(arr, low, high);
quick_sort(arr, low, parIdx - 1); // sort left subarray
quick_sort(arr, parIdx + 1, high); // sort right subarray
}
}
int main()
{
int A[] = {3, 5, 2, 13, 12, 2, 5};
int size = sizeof(A) / sizeof(int);
quick_sort(A, 0, size-1);
displayArray(A, size);
return 0;
}