快排,做个笔记
static void Main(string[] args) { var list = new List<int>() { 3, 4, 1, 2, 6, 5, 7 }; QuickSort(list, 0, list.Count - 1); foreach (var item in list) { Console.WriteLine(item); } } public static int Division(List<int> list, int left, int right) { while (left < right) { int num = list[left]; if (num > list[left + 1]) { list[left] = list[left + 1]; list[left + 1] = num; left++; } else { int temp = list[right]; list[right] = list[left + 1]; list[left + 1] = temp; right--; } } return left; } public static void QuickSort(List<int> list, int left, int right) { if (left < right) { int i = Division(list, left, right); QuickSort(list, i + 1,right); QuickSort(list, left, i - 1); } }
PS:转载请注明来源 https://www.cnblogs.com/sunshine-wy,疑问和勘误可在下方留言。