算法: 排序: 快速排序

 

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;
        }

       
View Code

 

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);
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/LeimOO/p/3798602.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值