zoukankan      html  css  js  c++  java
  • algorithm ch7 QuickSort

    快速排序是基于分治模式的排序,它将数组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部分示例如下:

    程序的简单实现如下:

    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。

  • 相关阅读:
    Python之MySQLdb
    Python 小方法
    Python文件打包
    禅道使用教程
    Linux命令
    安卓自动化测试monkey
    深入分析Java中的中文编码问题
    Linux命令搜索
    文件上传的类型选择控制
    MySql格式化日期函数
  • 原文地址:https://www.cnblogs.com/bestwangjie/p/4376287.html
Copyright © 2011-2022 走看看