快速排序 C语言实现

2017-12-12 08:25:55来源:CSDN作者:lancegentry人点击

分享

快排的总体思想如下:

if low < high 

找到枢轴点,使得枢轴点左侧小于枢轴点,右侧大于枢轴点

对左侧快排

对右侧快排

#include <stdio.h>//查找枢轴点位置int partition(int* a, int low, int high) {    int tmp = a[low];    while (low < high) {        while(low < high && a[high] >= tmp) --high;        a[low] = a[high];        while(low < high && a[low] <= tmp) ++low;        a[high] = a[low];    }    a[low] = tmp;    return low; }//快排void qsort(int* a, int low, int high) {    if (low < high) {        int p = partition(a, low, high);        qsort(a, low, p - 1);        qsort(a, p + 1, high);    }    return ;}int main() {    int n;    scanf("%d", &n);    int i = 0;    int a[100];    for (i = 0; i < n; ++i)        scanf("%d", &a[i]);        qsort(a, 0, n - 1);    for (i = 0; i < n; ++i)         printf("%d ", a[i]);    printf("/n");    return 0;}


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台