• 排序算法补充


    http://www.cnblogs.com/clingingboy/archive/2010/07/02/1770057.html

    一.计数排序

    思想:前提待排序元素是位于0到k之间的正整数

    用一个额外的数组(数组大小为k+1)记录计数数组元素

    如果元素x的位置为m,那么比元素x小的个数为m-1,按照以上思想先算出元素的计数索引,然后根据索引值从原待排序的数组中取元素,每次取元素,则计数减1

    http://www.cnblogs.com/sun/archive/2008/06/24/1228581.html

    算法参考如上地址

    /// <summary>
    /// counting sort
    /// </summary>
    /// <param name="arrayA">input array</param>
    /// <param name="arrange">the value arrange in input array</param>
    /// <returns></returns>
    public static int[] CountingSort(int[] arrayA, int arrange)
    {
        //array to store the sorted result, 
        //size is the same with input array.
        int[] arrayResult = new int[arrayA.Length];
    
        //array to store the direct value in sorting process
        //include index 0;
        //size is arrange+1;
        int[] arrayTemp = new int[arrange + 1];
    
        //clear up the temp array
        for (int i = 0; i <= arrange; i++)
        {
            arrayTemp[i] = 0;
        }
    
        //now temp array stores the count of value equal
        for (int j = 0; j < arrayA.Length; j++)
        {
            arrayTemp[arrayA[j]] += 1;
        }
    
        //now temp array stores the count of value lower and equal
        for (int k = 1; k <= arrange; k++)
        {
            arrayTemp[k] += arrayTemp[k - 1];
        }
    
        //output the value to result
        for (int m = arrayA.Length - 1; m >= 0; m--)
        {
            arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];
            arrayTemp[arrayA[m]] -= 1;
        }
    
        return arrayResult;
    }

    二.桶排序

    参考http://www.cnblogs.com/sun/archive/2008/07/07/1237331.html

    思路:前提待排序数组A[1...n]内的元素是随机分布在[0,1)区间内的的浮点数

    将原数组元素进行分类(分成n个链表(即桶的概念))

    然后将元素按取值不同分配到不同桶中

    对每个桶的数据进行排序

    然后对每个桶的数据重新按顺序收集合并即可

    算法参考如下http://blog.csdn.net/pojianbing/archive/2010/07/07/5718285.aspx

    /// <summary>
    /// 桶排序
    /// </summary>
    /// <param name="arrayToSort">待排序的数组(该数组的元素在[0-1) )</param>
    /// <returns>排序后的结果</returns>
    public static double[] BucketSort(double[] arrayToSort)
    {
        // 桶
        LinkedList<double>[] bucket = new LinkedList<double>[10];
        // 初始化桶
        for (int i = 0; i < 10; i++)
        {
            bucket[i] = new LinkedList<double>();
        }
    
        // 元素分装到各桶
        for (int i = 0; i < arrayToSort.Length; i++)
        {
            int bucketIndex = (int)(arrayToSort[i] * 10d);
    
            // 添加并进行插入排序
            InsertToLinkList(bucket[bucketIndex], arrayToSort[i]);
        }
    
        // 各桶排序
        int index = 0;
        for (int i = 0; i < 10; i++)
        {
            foreach (var item in bucket[i])
            {
                arrayToSort[index++] = item;
            }
        }
    
        return arrayToSort;
    }
    
    /// <summary>
    ///  按升序插入
    /// </summary>
    /// <param name="linkedList">要排序的链表</param>
    /// <param name="num">要插入排序的数字</param>
    private static void InsertToLinkList(LinkedList<double> linkedList, double num)
    {
        if (linkedList.Count == 0)
        {
            linkedList.AddFirst(num);
            return;
        }
    
        for (int i = linkedList.Count - 1; i >= 0; i--)
        {
            if (linkedList.ElementAt(i) <= num)
            {
                LinkedListNode<double> node = linkedList.FindLast(linkedList.ElementAt(i));
                linkedList.AddAfter(node, num);
            }
        }
    }
  • 相关阅读:
    模板 无源汇上下界可行流 loj115
    ICPC2018JiaozuoE Resistors in Parallel 高精度 数论
    hdu 2255 奔小康赚大钱 最佳匹配 KM算法
    ICPC2018Beijing 现场赛D Frog and Portal 构造
    codeforce 1175E Minimal Segment Cover ST表 倍增思想
    ICPC2018Jiaozuo 现场赛H Can You Solve the Harder Problem? 后缀数组 树上差分 ST表 口胡题解
    luogu P1966 火柴排队 树状数组 逆序对 离散化
    luogu P1970 花匠 贪心
    luogu P1967 货车运输 最大生成树 倍增LCA
    luogu P1315 观光公交 贪心
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1956471.html
Copyright © 2020-2023  润新知