-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
40 lines (37 loc) · 780 Bytes
/
Copy pathquick_sort.cpp
File metadata and controls
40 lines (37 loc) · 780 Bytes
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
#include<stdio.h>
int partition(int*,int,int);
void quicksort(int *array, int low,int high)
{
if(low >= high)
return;
int n = partition(array, low, high);
if(n > low+1)
quicksort(array,low,n-1);
if(high > n + 1)
quicksort(array, n + 1, high);
}
int partition(int *array,int low, int high)
{
int tmp = array[low];
while(low < high)
{
while(low < high &&array[high] > tmp)
high--;
if(low < high)
array[low] = array[high];
while(low < high &&array[low] <= tmp)
low++;
if(low < high)
array[high] = array[low];
}
array[high] = tmp;
return high;
}
int main()
{
int array[] = {5,4,3,2,1,6};
quicksort(array,0, sizeof(array)/sizeof(int)-1);
for(int i = 0; i < sizeof(array)/sizeof(int);i++)
printf("%d",array[i]);
return 0;
}