排序算法——快速排序

图示思想:

 

int Partition(int* a, int low, int high)
{
    int pivotkey = a[low];
    while (low < high)
    {
        while (low < high && a[high] >= pivotkey)
            high--;
        a[low] = a[high];
        while (low < high && a[low] <= pivotkey)
            low++;
        a[high] = a[low];
    }
    a[low] = pivotkey;
    return low;
}
void QuickSort(int* a, int low, int high)
{
    if (low < high)
    {
        int pivotloc = Partition(a, low, high);
        QuickSort(a, low, pivotloc-1);
        QuickSort(a, pivotloc+1, high);
    }
}

int main()
{
    int arr[] = { 9,2,3,1,5,4,7,8,6 };
    int n = sizeof(arr) / sizeof(int);

    int low = 0, high = n - 1;
    QuickSort(arr, low,high);


    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
    return 0;
}

 

posted @ 2019-08-14 21:00  Single_Dont  阅读(138)  评论(0编辑  收藏  举报