/*
* Copyright 2014 YU Heng-yang. All rights reserved.
*
* quick_sort2.c - Quick sort implementation 2.
*
* 2014-7-8 YU Heng-yang.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "common.h"
void quick_sort(int *arr, int n);
int main(int argc, char *argv[])
{
int *array, n;
n = input(&array);
quick_sort(array, n);
output(array, n);
return 0;
}
void quick_sort(int *arr, int n)
{
int i, j;
if (n < 2)
return;
assert(arr);
i = 0;
j = n;
do {
/* choose arr[0] as the pivot is not very good */
do ++i; while (i < j && arr[i] < arr[0]);
do --j; while (arr[j] > arr[0]);
if (i < j)
swap(arr, i, j);
} while (i < j);
swap(arr, 0, j);
quick_sort(arr, j);
quick_sort(arr + j + 1, n - j - 1);
}
快速排序2
最新推荐文章于 2021-11-19 16:57:43 发布