快速排序法

#include <iostream>
using namespace std;
void quick_sort(int s[], int low, int high);

void quick_sort(int s[], int low, int high)
{
    if(low < high)
    {
        int i = low, j = high, key = s[low];
        while (i < j )
        {
            while(i < j && s[j] >= key) 
            --j;  
            if(i < j) 
                s[i] = s[j];
                
            while(i < j && s[i] <= key) 
            ++i;
            if(i < j) 
                s[j] = s[i];
        }
        s[i] = key;
        quick_sort(s, low, i - 1);
        quick_sort(s, i + 1, high);
    }
}
int main()
{
    int i,a[] = {1,100,4,6,2,7,87,22,11,34,65,78,9,99};
    quick_sort(a,0,sizeof(a)/sizeof(a[0])-1);  //sizeof(a)/sizeof(a[0]) 数组元素个数
    cout << "快速排序法:" << endl;
    for(i = 0; i<sizeof(a)/sizeof(a[0]); i++)
        cout << a[i] << " " ;
    return 0;
}

 

posted @ 2014-12-11 15:53  ht-beyond  阅读(164)  评论(0编辑  收藏  举报