快速排序

#include "iostream"  
#include "vector"  
using namespace std;

void qSort(int a[], int l, int r) {
    if (l >= r)
        return;
    int i = l, j = r, key = a[l];
    while (i < j) {
        while (i < j && a[j] >= key) j--;
        if (i < j) a[i++] = a[j];
        while (i < j && a[i] < key) i++;
        if (i < j) a[j--] = a[i];
    }
    a[i] = key;
    qSort(a, l, i - 1);
    qSort(a, i + 1, r);
}

int main() {
    int a[] = {0, 1, 5, 2, 6, 7};
    qSort(a, 0, 5);
    for (int i = 0; i < 6; i++)
        cout << a[i] << " ";
    cout << endl;
}


 


 

發佈了13 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章