快速排序是基于分治模式的排序,它将数组a[p,...,r]分成两个子数组a[p,...q-1],a[q+1,...,r],使得a[p,...,q-1]中每个元素都小于a[q],而且小于等于a[q+1,...,r]中的元素,下标r也在计算中得到。它最坏的运行时间是o(n^2),但它的平均运行时间是o(nlgn)。其中分为partion和quicksort两个过程partion部分示例如下:

algorithm ch7  QuickSort

程序的简单实现如下:

int Partion(int a[], int iStart, int iEnd)
{
    int x = a[iEnd];
    int i = iStart - 1;
    for(int jLoop = iStart; jLoop != iEnd; ++jLoop)
    {
        if(a[jLoop] < x)
        {
            ++i;
            swap(a[i], a[jLoop]);
        }
    }
    swap(a[i+1], a[iEnd]);
    return i+1;
}

void QuickSort(int a[], int iStart, int iEnd)
{
    if(iStart < iEnd)
    {
        int iMiddle =  Partion(a, iStart, iEnd);
        QuickSort(a, iStart, iMiddle - 1);
        QuickSort(a, iMiddle + 1, iEnd);
    }
}

learn more , foolish more。

相关文章:

  • 2021-05-15
  • 2021-07-03
  • 2021-05-26
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
猜你喜欢
  • 2021-05-23
  • 2022-01-01
  • 2022-12-23
  • 2021-03-31
  • 2022-02-07
  • 2021-12-14
相关资源
相似解决方案