把数组的第一个数据作为比较的原点,比该数据小的数据排列在左边,比该数据大的数据排列在右边
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
void swap(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}
void _quick_sort(int array[], int start, int end)
{
int middle;
if(start > end)
return;
middle = get_middle1(array, start, end);
//middle = get_middle2(array, start, end);
_quick_sort(array, start, middle - 1);
_quick_sort(array, middle + 1, end);
}
int get_middle1(int array[], int start, int end)
{
int middle;
int change = array[start];
int loop = start + 1, head = 0, tail = end - start;
//int j = end;
int *p = malloc(sizeof(int) * (end - start + 1));
while(loop <= end)
{
if(array[loop] < change)
{
p[head] = array[loop];
head ++;
}
if(array[loop] > change)
{
p[tail] = array[loop];
tail --;
}
loop ++;
}
p[head] = change;
memmove(array + start, p, sizeof(int) * (end - start + 1));
return start + head;
}
int get_middle2(int array[], int start, int end)
{
int middle;
int change = array[start];
int i = start;
int j = end;
while(i < j)
{
while(i < j && array[j] >= change)
j --;
if(i < j)
{
array[i] = array[j];
i ++;
}
while(i < j && array[i] <= change)
i ++;
if(i < j)
{
array[j] = array[i];
j --;
}
}
array[j] = change;
return j;
}
void quick_sort(int array[], int length)
{
assert(NULL != array || 0 != length);
_quick_sort(array, 0, length -1);
}
void test()
{
int i;
int array[10] = {1,3,4,5,8,6,7,9,0,2};
quick_sort(array, 10);
for(i = 0; i < 10; i ++)
{
printf("%d, ", array[i]);
}
printf("
");
}
int main()
{
test();
}