快速排序是一种高效的排序算法,它采用分治法(Divide and Conquer)策略来对一个数组进行排序。以下是用C#实现的快速排序代码示例: ```csharp using System; class Program { static void Main() { int[] array = { 10, 7, 8, 9, 1, 5 }; QuickSort(array, 0, array.Length - 1); Console.WriteLine("Sorted array: " + string.Join(", ", array)); } static void QuickSort(int[] array, int low, int high) { if (low < high) { int pi = Partition(array, low, high); QuickSort(array, low, pi - 1); QuickSort(array, pi + 1, high); } } static int Partition(int[] array, int low, int high) { int pivot = array[high]; int i = (low - 1); // Index of smaller element for (int j = low; j <= high - 1; j++) { // If current element is smaller than or equal to pivot if (array[j] <= pivot) { i++; Swap(ref array[i], ref array[j]); } } Swap(ref array[i + 1], ref array[high]); return (i + 1); } static void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; } } ``` 逻辑点: 1. 递归调用:`QuickSort`函数通过递归调用自身来处理子数组。 2. 分区操作:`Partition`函数将数组分为两部分,并返回枢轴元素的最终位置。 3. 元素交换:`Swap`函数用于交换两个元素的位置。 [2025-04-21 16:41:25 | AI写代码神器 | 483点数解答]