标签: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