• 快速排序


     快速排序,算法并不稳定,需要优化,根据不稳定因素优化,涉及多个概念。

    快速排序

    堆积树

    大根堆

    小根堆

    完全二叉树

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace FastSort
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] array = { 49, 38, 65, 97, 76, 13, 27,88,77,33,22,11, };
                sort(array, 0, array.Length - 1);
                Console.ReadLine();
            }
    
            /**一次排序单元,完成此方法,key左边都比key小,key右边都比key大。
     
            **@param array排序数组 
     
            **@param low排序起始位置 
     
            **@param high排序结束位置
            **@return单元排序后的数组 */
    
            static int sortUnit(int[] array, int low, int high)
            {
                int key = array[low];
                while (low < high)
                {
                    /*从后向前搜索比key小的值*/
                    while (array[high] >= key && high > low)
                        --high;
                    /*比key小的放左边*/
                    array[low] = array[high];
                    /*从前向后搜索比key大的值,比key大的放右边*/
                    while (array[low] <= key && high > low)
                        ++low;
                    /*比key大的放右边*/
                    array[high] = array[low];
                }
                /*左边都比key小,右边都比key大。//将key放在游标当前位置。//此时low等于high */
                array[low] = key;
                foreach (int i in array)
                {
                    Console.Write("{0}	", i);
                }
                Console.WriteLine();
                return high;
            }
    
            /**快速排序 
            *@paramarry 
            *@return */
            public static void sort(int[] array, int low, int high)
            {
                if (low >= high)
                    return;
                /*完成一次单元排序*/
                int index = sortUnit(array, low, high);
                /*对左边单元进行排序*/
                sort(array, low, index - 1);
                /*对右边单元进行排序*/
                sort(array, index + 1, high);
            }
        }
    }

    以上代码复制自百度百科,自己调试使用。

  • 相关阅读:
    TLE: poj 1011 Sticks
    UVa 116 Unidirectional TSP
    csuoj 1215 稳定排序
    UVa 103 Stacking Boxes
    UVa 147 Dollars
    UVa 111 History Grading
    怎么在ASP.NET 2.0中使用Membership
    asp.net中如何删除cookie?
    ASP.NET中的HTTP模块和处理程序[收藏]
    NET开发中的一些小技巧
  • 原文地址:https://www.cnblogs.com/Tpf386/p/8066052.html
Copyright © 2020-2023  润新知