c++ 快速排序

来源:互联网 发布:淘宝店铺设计师 编辑:程序博客网 时间:2024/06/07 02:19

直接上代码

/**************************   * File Name: quick-sort-p96.cpp * Author: No One   * E-mail: 1130395634@qq.com   * Created Time: 2017-02-21 22:40:40**************************/#include <iostream>using namespace std;int main(){    void quickSort(int *, int s, int e);    int a[] = {13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11};    int n = sizeof(a)/sizeof(int);    cout << "排序前:";    for(int i = 0; i < n; i++){        cout << a[i] << "\t";    }    cout << endl;    quickSort(a, 0, n);    cout << "排序后:";    for(int i = 0; i < n; i++){        cout << a[i] << "\t";    }       cout << endl;}int partition(int *a, int s, int e){    int x = a[e-1];    int i = s - 1;    for(int j = s; j < e - 1; j++){        if(a[j] < x){            i++;            int tmp = a[i];            a[i] = a[j];            a[j] = tmp;        }    }    a[e - 1] = a[i + 1];    a[i + 1] = x;    return i + 1;}void quickSort(int *a, int s, int e){    if(s < e){        int p = partition(a, s, e);        quickSort(a, s, p);        quickSort(a, p + 1, e);    }}
0 1
原创粉丝点击