-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.c
51 lines (44 loc) · 994 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
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void quicksort(int *list, int size)
{
int *endPtr = list + size - 1;
int *headPtr = list;
while (headPtr < endPtr)
{
if (*headPtr > *endPtr) {
swap(endPtr, endPtr - 1);
if (endPtr - headPtr > 1)
swap(endPtr, headPtr);
endPtr--;
} else {
headPtr++;
}
}
if (size > 2) {
quicksort(list, endPtr - list);
quicksort(endPtr + 1, size - (endPtr - list + 1));
}
}
int main(int argc, char *argv[])
{
int n;
int *list;
printf("n: ");
scanf("%d", &n);
list = (int *) malloc(n * sizeof(int));
printf("Data Entry:\n");
for (int i = 0; i < n; i++)
scanf("%d", list + i);
quicksort(list, n);
printf("Data Sort:\n");
for (int i = 0; i < n; i++)
printf("%d\n", list[i]);
return 0;
}