• 快速排序


    快速排序使用归并思想,对于子数组A[p..r],有如下三步分治过程:

    1. 分解:划分为A1[p..q-1]和A2[q+1..r],使得A1元素比A[q]小,A2元素比A[q]大
    2. 解决:递归调用快速排序,对分解的数组A1和A2进行排序
    3. 合并:因为子数组都是原址排序,不需要排序,数组A[p..r]已经有序
    static void QuickSort(List<int> sq, int s, int e)
    {
        if (s < e)
        {
            int st = Partition(sq, s, e);
            QuickSort(sq, s, st-1);
            QuickSort(sq, st+1, e);
        }
    }

    其中Partition部分将会选择一个元素作为主元(一般是最后一个),将元素分割两个部分,前边一部分小于主元,后边一部分大于主元

    static int Partition(List<int> sq, int s, int e)
    {
        int pivotElement = sq[e];
        int i = s - 1;
        for (int j = s; j < e; j++)
        {
            if (sq[j] <= pivotElement)
            {
                i++;
                int temp1 = sq[i];
                sq[i] = sq[j];
                sq[j] = temp1;
            }
        }
        i++;
        int temp = sq[i];
        sq[i] = sq[e];
        sq[e] = temp;
        return i;
    }

    使用快速排序,在最坏的情况下为Θ(n^2),期望运行时间为Θ(nlg(n)).期望运行时间是在输入序列的所有排列都是等概率的,但工程中,并不总是成立,为此可以引入随机化。

    随机化中,将随机的以为元素与最后一个元素交换。

    static void RandomizedQuickSort(List<int> sq, int s, int e)
    {
        if (s < e)
        {
            int st = RandomizedPartition(sq, s, e);
            RandomizedQuickSort(sq, s, st - 1);
            RandomizedQuickSort(sq, st + 1, e);
        }
    }
    static int RandomizedPartition(List<int> sq, int s, int e)
    {
        int i = rand.Next(s, e);
        int temp = sq[i];
        sq[i] = sq[e];
        sq[e] = temp;
        return Partition(sq, s, e); 
    }
  • 相关阅读:
    c ++ auto 的使用
    poj 3169 Layout
    1076 Forwards on Weibo (30)(30 分)
    Zookeeper注册节点的掉线自动重新注册及测试方法
    ZooKeeper(3.4.5) 使用Curator监听事件
    Nginx 引入线程池,提升 9 倍性能
    面试总结 地址
    struts2原理
    struts2拦截器与过滤器
    java网络编程serversocket
  • 原文地址:https://www.cnblogs.com/qiusuo/p/5211359.html
Copyright © 2020-2023  润新知