快速排序的一种实现

来源:互联网 发布:仿58同城网微招聘源码 编辑:程序博客网 时间:2024/05/20 23:36
#include <stdio.h>void quickSort(int * a, int low, int high);int findPos (int * a, int low, int high);int main(){    int a[7] = {2, 5, 7, 8, 5, 4, 1};    int i;    quickSort(a, 0, 6);    for(i=0; i<7; i++){        printf("%d    ", a[i]);    }    printf("\n");    getchar();    return 0;}void quickSort(int * a, int low, int high){    int pos;    if(low < high){        pos = findPos(a, low, high);        quickSort(a, low, pos-1);        quickSort(a, pos+1, high);    }}int findPos (int * a, int low, int high){    int val = a[low];    while(low < high){        while(low < high && a[high] >= val){            --high;        }        a[low] = a[high];        while(low < high && a[low] <= val){            ++low;        }        a[high] = a[low];    }    a[low] = val;    return low;}
0 0
原创粉丝点击