-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.c
72 lines (59 loc) · 1.37 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
61
62
63
64
65
66
67
68
69
70
71
72
// QuickSort
//
// Created by ciqu on 2018/12/19.
// Copyright © 2018年 ciqu. All rights reserved.
//
#include <stdio.h>
//print array
void print_array(int list[],int length,char *print_string){
printf("%s", print_string);
for (int i = 0; i < length ; i++){
printf("%d\t",list[i]);
}
printf("\n");
}
//exchange two number in array
void swap(int array[], int a, int b)
{
int temp;
temp = array[a];
array[a] = array[b];
array[b] = temp;
return ;
}
void quicksort(int array[],int begin,int end)
{
int i,j;
if(begin<end)
{
i = begin+1;
j = end;
while (i<j)
{
if (array[i]>array[begin])
{
swap(array, i, j);
j--;
}
else
{
i++;
}
}
if (array[i]>=array[begin]) //identify the location of number array[begin]
{
i--;
}
swap(array, begin, i);
quicksort(array, begin, i);
quicksort(array, j, end);
}
}
int main(int argc, const char * argv[]) {
int array[] = {-1,100,25,16,2,23,33,95,34,0};
int arrlen = 10;
print_array(array, arrlen,"array unsorted:\n");
quicksort(array, 0, arrlen-1); // quicksort
print_array(array, arrlen,"array after quicksort:\n");
return 0;
}