-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.c
More file actions
62 lines (47 loc) · 1.02 KB
/
quick_sort.c
File metadata and controls
62 lines (47 loc) · 1.02 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
#include<stdio.h>
void quicksort(int arr[], int l, int h);
int partition(int arr[], int l, int h);
void swap(int *a, int *b);
int main()
{
int arr[] = {5, 8, 2, 11, 26, 9, 8, 7, 19};
int len = sizeof(arr) / sizeof(int);
quicksort(arr, 0, len - 1);
int i;
for(i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
getch();
return 0;
}
void quicksort(int arr[], int l, int h)
{
if(l < h)
{
int p = partition(arr, l, h);
quicksort(arr, l, p - 1);
quicksort(arr, p + 1, h);
}
}
int partition(int arr[], int l, int h)
{
int i = l - 1, x = arr[h], j;
for(j = l; j < h; j++)
{
if(arr[j] < x)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[h]);
return i + 1;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}