快速排序(Quicksort)是对冒泡排序的一种改进,由 C. A. R. Hoare 在 1962 年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
#region 生成随机int数组 privatestaticint[] RandomNums(int Length, int Count) { Random Rand = new Random(7); int[] Nums = newint[Count]; for (int c = 0; c < Count; c++) { string Result = string.Empty; for (int i = 0; i < Length; i++) { Result += Rand.Next(10).ToString(); } Nums[c] = Convert.ToInt32(Result); } return Nums; } #endregion
#region 排序算法:快速排序 privatestaticvoidQuickSort(refint[] Nums, int low, int high) { int key = Nums[low]; int i = low; int j = high; while (low < high) { while (low < high) { if (Nums[high] < key) { Nums[low] = Nums[high]; Nums[high] = key; break; } else { high--; } } while (low < high) { if (Nums[low] > key) { Nums[high] = Nums[low]; Nums[low] = key; break; } else { low++; } } if (i < low - 1) { QuickSort(ref Nums, i, low - 1); } if (j > high + 1) { QuickSort(ref Nums, high + 1, j); } } } #endregion