码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序(待解析)

时间:2019-09-16 14:16:15      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:ror   als   str   array   error   exce   line   env   dex   

using System;
using System.Collections.Generic;

namespace QuickSort
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            List<int> array = new List<int>() { 1, 12, 23, 44, 555, 666, 54, 23, 546, 45, 23, 45, 76, 87, 99, 33, 22, 454, 123, 2345, 77 };
            ConsoleExtension.ListWriteLine<int>(array);
            QuickSort(array);
            ConsoleExtension.ListWriteLine<int>(array);
            Console.ReadLine();
        }

        public static void QuickSort(List<int> source)
        {
            QuickSort(source, 0, source.Count - 1);
        }

        public static void QuickSort(List<int> source, int left, int right)
        {
            if (left < right)
            {
                var temp = Partition(source, left, right);

                QuickSort(source, left, temp - 1);

                QuickSort(source, temp + 1,right);
            }
        }

        public static int Partition(List<int> source, int left, int right)
        {
            var temp = source[left];
            var index = left + 1;
            for (int i = left + 1; i <= right; i++)
            {
                if (source[i] < temp)
                {
                    ChangePositon(source, index, i);
                    index++;
                }
            }

            ChangePositon(source, index - 1, left);

            return index - 1;
        }

        public static void ChangePositon(List<int> source,int indexA,int indexB)
        {
            if (indexA == indexB)
            {
                return;
            }
            var temp = source[indexA];
            source[indexA] = source[indexB];
            source[indexB] = temp;
        }
    }

    public static class ConsoleExtension
    {
        public static void ListWriteLine<T>(List<T> source, bool isNeedError = false)
        {
            if (source != null)
            {
                for (int i = 0; i < source.Count; i++)
                {
                    Console.Write(source[i].ToString());
                    if (i < source.Count - 1)
                    {
                        Console.Write(",");
                    }
                    else
                    {
                        // 注意跨平台问题,c#使用跨平台方法避免在centos,windows下不一致
                        Console.Write(System.Environment.NewLine);
                    }
                }
            }
            else
            {
                if (isNeedError)
                {
                    throw new Exception("null of your source");
                }
            }
        }
    }
}

  

快速排序(待解析)

标签:ror   als   str   array   error   exce   line   env   dex   

原文地址:https://www.cnblogs.com/lsnct/p/11526587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!