算法: 排序: 快速排序

1. Algrithom

?Given an array of values, pick a value as a pivot value

?Check each value against the pivot value and

- bring each value higher than the pivot value to the right of the pivot value: GreaterList

- bring each value lower than or equal to the pivot to the left of the pivot value: LessList

? Recursively call the algorithm for the array left and the array right of the pivot (which is now in the right spot).

? Combine LessList + pivot + GreaterList.

2. Implement

  public static void TestSimpleQuickSort()
        {
            List<int> intArray = TestData.GetListFromString();
            List<int> result = new List<int>();
            TimeWatcher.StartWatchFunc(SimpleQuickSort, intArray, out result);
            //TimeWatcher.StartWatchDelegate(SimpleQuickSort, intArray, out result);
        }

        public static List<int> SimpleQuickSort(List<int> a)
        {
            Random r = new Random();
            List<int> less = new List<int>();
            List<int> greater = new List<int>();
            if (a.Count <= 1)
                return a;
            //int pos = r.Next(a.Count);
            int pos = a.Count/2;

            int pivot = a[pos];
            a.RemoveAt(pos);
            foreach (int x in a)
            {
                if (x <= pivot)
                {
                    less.Add(x);
                }
                else
                {
                    greater.Add(x);
                }
            }
            return Concat(SimpleQuickSort(less), pivot, SimpleQuickSort(greater));
        }

        private static List<int> Concat(List<int> less, int pivot, List<int> greater)
        {
            List<int> sorted = new List<int>(less);
            sorted.Add(pivot);
            foreach (int i in greater)
            {
                sorted.Add(i);
            }

            return sorted;
        }

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.CA.Algorithm.Sorting
{
    public class TimeWatcher
    {
        public delegate List<int> TestDelegate(List<int> array);

        public static void StartWatchAction( Action<List<int>> func, List<int> array)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            func(array);

            watch.Stop();

            Console.WriteLine(watch.Elapsed);
        }

        public static void StartWatchDelegate(TestDelegate func, List<int> array, out List<int> result)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            result = func(array);

            watch.Stop();

            Console.WriteLine(watch.Elapsed);
        }

        public static void StartWatchFunc(Func<List<int>, List<int>> func, List<int> array, out List<int> result)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            result = func(array);

            watch.Stop();

            Console.WriteLine(watch.Elapsed);
        }
    }
}

算法: 排序: 快速排序

时间: 2024-08-28 18:04:46

算法: 排序: 快速排序的相关文章

排序算法 之 快速排序

快速排序是基于分治思想的一种排序算法,就像该方法的名字一样,速度比较快,所以叫做快速排序:它的平均时间复杂度为O(N*logN),最坏时间复杂度为O(n2),由于快速排序在序列元素数量多的时候速度比较快,所以很多语言内置的排序方法也是用快速排序实现的.快速排序也有很多优化的版本,比如在排序时基数的选择等等-下面就说一下一般的快速排序的实现. 基本思想: 快速排序的基本思想就是,先从待排序的序列中任选一个元素作为基数,然后将序列中的其他小于基数的元素放在基数的左边,大于或等于基数的元素放在基数的右

基础算法之排序--快速排序

1 /************************************************************************************** 2 * Function : 快速排序 3 * Create Date : 2014/04/21 4 * Author : NTSK13 5 * Email : [email protected] 6 * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性. 7 * 任何单位和个人不经本人允许不

啊哈!算法之快速排序与桶排序

啊哈!算法之快速排序与桶排序 1.快速排序算法 快速排序由 C. A. R. Hoare(东尼·霍尔,Charles Antony Richard Hoare)在1960 年提出,之后又有许多人做了进一步的优化.在数列种随机找出一个基准数,因为数列是杂乱的,所以取首项为基准数.从后往前找到比基准数大的位置,再从前往后找到比基准数小的位置,交换元素:右游标向前移动与左游标向后移动,它们相遇时用基准数的位置与相遇的位置交换.此时原来数列以相遇的位置被划分为了两个需要排序的数列,再次执行上述过程:当左

排序算法 一 快速排序

算法:快速排序 思想:通过一趟排序,确定了一个data的最终确切位置.(通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列)  特点:不稳定算法. 代码实现:

排序——快速排序算法

快速排序的思想在程序中经常用到,虽然C++给出了快速排序的函数调用,但是很多程序可能需要自己写排序过程,快速排序就会被用到,以下是快速排序算法: 快速排序时间复杂度是O(nlog(n)),在数据有序的情况下最耗时 (程序输入如果使用scanf_s则编译器为vs2013) #include<stdio.h> #include<stdlib.h> #define  MAXSIZE  1<<9 typedef struct { int key; }RedType; typed

常用排序算法之——快速排序(C语言+VC6.0平台)

经典排序算法中快速排序具有较好的效率,但其实现思路相对较难理解. #include<stdio.h> int partition(int num[],int low,int high) //以key为基准 将待排数列“高”.“低 ”两部分,“高”部分的所有数据比key大,“低”部分的数据都比key小 { int left,right,key; left=low;right=high;key=num[low]; while(left<right) { while(left<right

常用排序算法之——快速排序

快速排序的原理: 首先找一个标兵值,等于某一个元素值:遍历数组,将数组分为小于标兵值和大于标兵值的两部分:然后分别对两个部分采用快速排序,递归. 分开数组时,维持一个指针,指向已找到小部分的最后一个元素:一个指针用于遍历. 不稳定排序算法.当数组已经有序时,时间复杂度最差,为O(N2),平均.最优情况下都为O(N lgN). 代码如下: 1 #include <iostream> 2 using namespace std; 3 4 template<typename T> 5 v

排序算法系列——快速排序

记录学习点滴 快速排序算法是一种很有趣的算法,短小精悍,性能强劲,对于大部分情况都可以胜任,但对极端环境难以应付. 快速排序我理解为:这是一个“以自我为中心的”+“分治法”思想的算法. 分治法不必多说,化繁为简,那就是逐个击破. 那什么是“以自我为中心”?顾名思义,就是每次都一个“我”,每个人都要围绕“我”行事,比“我”小的都去左边站着,比“我”他大的都去右边站着,而且“我”不去关心每一边都有谁,反正你没“我”大或者小就行.一旦“我”落位了妥帖了,“我”就不动了.然后再在左右两边分别产生新“我”

排序算法之快速排序Java实现

排序算法之快速排序 舞蹈演示排序: 冒泡排序: http://t.cn/hrf58M 希尔排序:http://t.cn/hrosvb  选择排序:http://t.cn/hros6e  插入排序:http://t.cn/hros0W  快速排序:http://t.cn/ScTA1d  归并排序:http://t.cn/Sc1cGZ 快速排序是一个就地排序,分而治之,大规模递归的算法.从本质上来说,它是归并排序的就地版本. 1.快速排序可以由下面四步组成:(1) 如果不多于1个数据,直接返回.(2

经典排序算法之快速排序(C语言版)

快速排序是一种很常用的排序算法. /*  * 快速排序(伪算法) 2016-04-20 23:34:16  * 1.先找到第一个元素的最终位置  * 2.对第一个元素的最终位置之前的元素,进行快速排序.  * 3.对第一个元素的最终位置之后的元素,进行快速排序.  * */ extern void QuickSort(int a[],int low,int high);//第二个参数表示第一个元素的下标,第三个参数表示最后一个元素的下标 extern int FindPos(int a[], i