forked from Flyfishering/algorithmDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.c
More file actions
71 lines (56 loc) · 1.12 KB
/
Copy pathquick_sort.c
File metadata and controls
71 lines (56 loc) · 1.12 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
68
69
70
71
//
// quick_sort.c
// algorithm
//
// Created by wangbinbin on 2019/1/16.
// Copyright © 2019 wangbinbin. All rights reserved.
//
#include "quick_sort.h"
void quick_sort_dump(int *arr, int size)
{
int idx;
for (idx = 0; idx < size; idx++)
printf("%08d\n", arr[idx]);
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int partition(int *arr, int p, int r)
{
//int pivot = arr[r];
int i, j;
i = j = p;
for (; j < r; j++) {
if (arr[j] < arr[r]) {
if(i != j)
{
swap(arr + i, arr + j);
}
i++;
}
}
swap(arr + i, arr + r);
return i;
}
void __quick_sort(int *arr, int p, int r)
{
int q;
if (p >= r)
return;
q = partition(arr, p, r);
__quick_sort(arr, p, q-1);
__quick_sort(arr, q+1, r);
}
void quick_sort(int *arr, int size)
{
__quick_sort(arr, 0, size - 1);
}
void quick_sort_test()
{
int test[10] = {5, 8, 9, 23, 67, 1, 3, 7, 31, 56};
quick_sort(test, 10);
quick_sort_dump(test, 10);
}