算法实例


##排序算法Sort##
### 快速排序QuickSort ###
bing搜索结果
http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&mkt=zh-cn&FORM=BKACAI

 *使用队列*
 QuickSort排序中其实最贴近人类思考方式的实现是利用队列技术
 1.建立左右队列
 2.遍历List,小于Pivot的放入左队列,大于等于Pivot的放入右队列
 3.左队列出队+Pivot+右队列出队  构造成一个第一次排序的List
 4.左队列重复步骤123,右队列重复123
 5.跳出循环的条件是队列为空
 

 *使用指针对*
 1.将List尾部的元素設置為pivot
 2.設置一對指針,其中wallIndex指針標誌小於pivot的數,循環指針標誌遍歷的位置
 3.Note:關鍵算法在於List中想要比較移動元素需要兩組指針,wallIndex用於定位需要插入的位置,循環指針用於遍歷元素.
 4.但是以文中算法其實是QuickSort的變種模式,如圖我們如果以List最後的元素作為pivot的話,第一次排序結果因該是{49 38 13 27}49{65 97 76} 但是實際使用的排序算法導致的結果應該為 {49 38 13 27}49{76 65 97}
 5.使用變種的算法優勢在於使用的一對指針,實際減少了內存的使用和交換的開銷
 6.如果使用隊列技術,實際上額外使用了兩塊內存空間,但是其優勢在于可以更加的貼近人類的思維習慣

### 代碼展示 ###

#### 使用指針對 ####
https://github.com/aalhour/C-Sharp-Algorithms/blob/master/Algorithms/Sorting/QuickSorter.cs

 /// <summary>
 ///
 /// </summary>
 public static class QuickSorter
 {

     /// <summary>
     /// The public APIs for the quick sort algorithm.
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="collection"></param>
     /// <param name="comparer"></param>
     public static void QuickSort<T>(this IList<T> collection, Comparer<T> comparer = null)
     {
         int startIndex = 0;
         int endIndex = collection.Count - 1;

         // If the comparer is Null, then initialize it using a default typed comparer
         comparer = comparer ?? Comparer<T>.Default;

         collection.InternalQuickSort(startIndex, endIndex, comparer);
     }

     /// <summary>
     /// The recursive quick sort algorithm
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="collection"></param>
     /// <param name="leftmostIndex"></param>
     /// <param name="rightmostIndex"></param>
     /// <param name="comparer"></param>
     private static void InternalQuickSort<T>(this IList<T> collection, int leftmostIndex, int rightmostIndex, Comparer<T> comparer)
     {
         // Recursive call check
         if (leftmostIndex < rightmostIndex)
         {
             int wallIndex = collection.InternalPartition(leftmostIndex, rightmostIndex, comparer);
             collection.InternalQuickSort(leftmostIndex, wallIndex - 1, comparer);
             collection.InternalQuickSort(wallIndex + 1, rightmostIndex, comparer);
         }
     }

     // The partition function, used in the quick sort algorithm
     /// <summary>
     ///  The partition function, used in the quick sort algorithm
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="collection"></param>
     /// <param name="leftmostIndex"></param>
     /// <param name="rightmostIndex"></param>
     /// <param name="comparer"></param>
     /// <returns></returns>
     private static int InternalPartition<T>(this IList<T> collection, int leftmostIndex, int rightmostIndex, Comparer<T> comparer)
     {
         int wallIndex, pivotIndex;

         // Choose the pivot
         pivotIndex = rightmostIndex;
         T pivotValue = collection[pivotIndex];

         // Compare remaining array elements against pivotValue
         wallIndex = leftmostIndex;

         // Loop until pivot: exclusive!
         for (int i = leftmostIndex; i <= (rightmostIndex - 1); i++)
         {
             // check if collection[i] <= pivotValue
             if (comparer.Compare(collection[i], pivotValue) <= 0)
             {
                 collection.Swap(i, wallIndex);
                 wallIndex++;
             }
         }

         collection.Swap(wallIndex, pivotIndex);

         return wallIndex;
     }

 }

#### 使用隊列 ####

 /// <summary>
 /// using Queue for quick sort
 /// </summary>
 public static class QuickSorterA
 {
     public static void QuickSortA<T>(this IList<T> collection, Comparer<T> comparer = null)
     {
         // If the comparer is Null, then initialize it using a default typed comparer
         comparer = comparer ?? Comparer<T>.Default;
         Queue<T> _queue = new Queue<T>(collection);
         _queue.InternalQuickSortA(comparer);
         collection.Clear();
         foreach (var item in _queue)
         {
             collection.Add(item);
         }

     }

     private static void InternalQuickSortA<T>(this Queue<T> collection, Comparer<T> comparer)
     {
         if (collection.Count <=0)
         {
             return;
         }
         // Recursive call check
         Queue<T> _leftQueue = new Queue<T>();
         Queue<T> _rightQueue = new Queue<T>();
         T _povit = collection.Dequeue();
         foreach (var item in collection)
         {
             if (comparer.Compare(item, _povit) <= 0)
             {
                 _leftQueue.Enqueue(item);
             }
             else
             {
                 _rightQueue.Enqueue(item);
             }
         }

         _leftQueue.InternalQuickSortA<T>(comparer);
         _rightQueue.InternalQuickSortA<T>(comparer);

         collection.Clear();
         foreach (var item in _leftQueue)
         {
             collection.Enqueue(item);
         }

         collection.Enqueue(_povit);

         foreach (var item in _rightQueue)
         {
             collection.Enqueue(item);
         }
     }
 }

測試用例

[TestMethod]
    public void TestMethod1()
    {
        List<long> list = new List<long>() { 23, 42, 4, 16, 8, 15, 3, 9, 55, 0, 34, 12, 2, 46, 25 };
        list.QuickSort();
        List<long> listA = new List<long>() { 23, 42, 4, 16, 8, 15, 3, 9, 55, 0, 34, 12, 2, 46, 25 };
        listA.QuickSortA();
    }

算法实例-C#-快速排序-QuickSort的更多相关文章

  1. 排序算法四:快速排序(Quicksort)

    快速排序(Quicksort),因其排序之快而得名,虽然Ta的平均时间复杂度也是O(nlgn),但是从后续仿真结果看,TA要比归并排序和堆排序都要快. 快速排序也用到了分治思想. (一)算法实现 pr ...

  2. 小小c#算法题 - 6 - 快速排序 (QuickSort)

    快速排序是排序算法中效率比较高的一种,也是面试常被问到的问题. 快速排序(Quick Sort)是对冒泡排序的一种改进.它的基本思想是,通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字 ...

  3. 快速排序(quicksort)算法实现

    快速排序(quicksort)是分治法的典型例子,它的主要思想是将一个待排序的数组以数组的某一个元素X为轴,使这个轴的左侧元素都比X大,而右侧元素都比X小(从大到小排序).然后以这个X在变换后数组的位 ...

  4. 归并排序(MergeSort)和快速排序(QuickSort)的一些总结问题

    归并排序(MergeSort)和快速排序(QuickSort)都是用了分治算法思想. 所谓分治算法,顾名思义,就是分而治之,就是将原问题分割成同等结构的子问题,之后将子问题逐一解决后,原问题也就得到了 ...

  5. javascript常用经典算法实例详解

    javascript常用经典算法实例详解 这篇文章主要介绍了javascript常用算法,结合实例形式较为详细的分析总结了JavaScript中常见的各种排序算法以及堆.栈.链表等数据结构的相关实现与 ...

  6. 快速排序(QuickSort)

    1.算法思想    快速排序是一种划分交换排序.它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod). (1) 分治法的基本思想    分治法的基本思想是:将原 ...

  7. json数据中的某一个字段进行快速排序quicksort

    快速排序(Quicksort)是对冒泡排序的一种改进,是一种分而治之算法归并排序的风格. 核心的思想就是通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小 ...

  8. 排序算法-Java实现快速排序算法

  9. Adaboost 算法实例解析

    Adaboost 算法实例解析 1 Adaboost的原理 1.1 Adaboost基本介绍 AdaBoost,是英文"Adaptive Boosting"(自适应增强)的缩写,由 ...

随机推荐

  1. hibernate HQL和Criteria

    package com.test; import java.util.Date; import java.util.List; import org.hibernate.Query; import o ...

  2. 爱上MVC~ajax调用分部视图session超时页面跳转问题

    回到目录 这个问题出现了很多年了,都没有解决,问题是这样的,有一个需要授权才可以访问的分部视图,在一个view中使用ajax的方法去调用它,然后更新页面的局部DIV,这时,如果你长时间不操作,sess ...

  3. Atitit 通过调用gui接口杀掉360杀毒 360卫士  qq保镖等难以结束的进程(javac# php )

    Atitit 通过调用gui接口杀掉360杀毒 360卫士  qq保镖等难以结束的进程(javac# php ) 1.1. 这些流氓软件使用操作系统os提供的普通api根本就杀不掉啊1 1.2. 使用 ...

  4. Vagrant使用简介

    一.简介: Vagrant是一款用于构建及配置虚拟开发环境的软件,基于Ruby,主要以命令行的方式运行.主要使用Oracle的开源VirtualBox虚拟化系统,与Chef,Salt,Puppet等环 ...

  5. 每天一个linux命令(11):nl命令

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...

  6. MVC3中如何输出富文本

    MVC3中如何输出富文本 在网站的文本输出中,经常会将DB里的文本输出到页面上. 一般来说是直接利用MVC3中的ViewBag将文本带到前台并表示, 或是是直接以<%:model.data%&g ...

  7. TSql HierarchyID 数据类型用法

    HierarchyID 数据类型是一种长度可变的系统数据类型.可使用 HierarchyID 表示层次结构中的位置.类型为 HierarchyID 的列不会自动表示树,由应用程序来生成和分配 Hier ...

  8. silverlight中 Storyboard(动画)的使用,实现球的上下循环移动,左右移动,及旋转功能

    话说,总结应该是个收获的心情,可现在的自己似乎没感觉到哪个喜悦的心情,说明自己做得还不够好,现在还是把一些做好的东西总结下.“总结”是学习,工作中必须的,不能偷这个懒 o(╯□╰)o 实现上下循环移动 ...

  9. Js内存回收

    Javascript的世界中,隐藏了很多内存陷阱,不能得到合理释放的内存会埋下各种隐患,本文旨在以实用角度去解读Js涉及到的内存,且看勇士如何斗恶龙~ javascript 内存 回收 本文可以看做是 ...

  10. poj1703Find them, Catch them(并查集以及路径压缩)

    /* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...