快速排序之三青年版
/// <summary> /// Quick Sort /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> public static void QuickSort<T>(T[] array,int left,int right) where T : IComparable { if (left < right) { int middle = GetMiddleFroQuickSort<T>(array, left, right); QuickSort<T>(array, left, middle - 1); QuickSort<T>(array, middle + 1, right); } } /// <summary> /// get the index of the middle value for qucik sort /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="left"></param> /// <param name="right"></param> /// <returns></returns> private static int GetMiddleFroQuickSort<T>(T[] array, int left, int right) where T : IComparable { T key = array[left]; while (left < right) { while (left < right && key.CompareTo(array[right]) < 0) { right--; } if (left < right) { T temp = array[left]; array[left] = array[right]; //Console.WriteLine("array[{0}]:{1} ----> arry[{2}]:{3}", left, temp, right, array[right]); left++; } while (left < right && key.CompareTo(array[left]) > 0) { left++; } if (left < right) { T temp = array[right]; array[right] = array[left]; //Console.WriteLine("array[{0}]:{1} ----> arry[{2}]:{3}", right, temp, left, array[left]); right--; } array[left] = key; } //Console.WriteLine("find the middle value {0} and the index {1}", array[left], left); return left; } 测试代码:int[] array = new int[] {43,23,80,15,789,27,90,69,66,158,45,32,1,22,77,66,44}; SortHelper.QuickSort<int>(array,0,array.Length-1); Console.WriteLine("after quick sort"); foreach (int i in array) { Console.Write(i + "->"); } Console.WriteLine();
文艺青年是这样子写的:
/// <summary> /// 快速排序函数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> IEnumerable<T> QuickSorting<T>(IEnumerable<T> list) where T : IComparable<T> { if (list.Count() <= 1) return list; var pivot = list.First(); return QuickSorting(list.Where(x => x.CompareTo(pivot) < 0)) .Concat(list.Where(x => x.CompareTo(pivot) == 0)) .Concat(QuickSorting(list.Where(x => x.CompareTo(pivot) > 0))); } 测试代码: static void Main(string[] args) { List<int> ints = new List<int> { ,33,11,43,55,123,452,1,3,5,15,153,10000,532,553,22,33,11,53,11,33 }; List<int> lists = new Program().QuickSorting<int>(ints).ToList<int>(); lists.ForEach(A => Console.WriteLine(A)); }
二逼青年竟是这样子写的:
private void checkNumOredASC(ref int[] num, int left, int right) { int i = left; int j = right; int temp; int middle; middle = num[(right + left) / 2]; while ((num[i] < middle) && (i < right)) { i++; } while ((num[j] > middle) && (j > left)) { j--; } if (i <= j) { temp = num[i]; num[i] = num[j]; num[j] = temp; i++; j--; } if (left < j) { checkNumOredASC(ref num, left, j); } if (right > i) { checkNumOredASC(ref num, i, right); } } 测试代码: int[] a = new int[] {23,1,34,134,2,4,563,64,232,6,4,75,754, }; Program p = new Program(); p.checkNumOredASC(ref a,开头索引,结尾索引); foreach (var item in a) { Console.WriteLine(item); }
人有三等:第三流的人,一辈子看不透人性本质与商场的游戏则,他们皆深陷在错觉与梦幻中
第二流的人,是看透了,明白了,但是却不能自拔,甚至是无可奈何
第一流的人,是不仅看透想通,并且开始学会怎样玩这场游戏,用怎样的技巧来应对你的人生