C# 快速排序

将数组中的第一个元素作为key, 依次和后边的元素进行比较,如果有元素小于key ,将 key到小于key元素之间的这部分元素向后移动1,则将小于key的元素移到key的位置,这样能保证key之前的元素始终小于key  ,遍历结束后 key之前的元素都小于key,key之后的元素都大于key  然后利用递归的思想   进行代码如下

static void Main(string[] args)
{
int[] num = new int[] { 5, 7, 8, 3, 2, 9, 6, 4, 10, 1 };
QuickSort(num, 0, num.Length - 1);
for (int i = 0; i < num.Length; i++)
{
Console.Write(num[i] + " ");
}
Console.ReadKey();
}

public static void QuickSort(int[] num, int start, int end)
{
if (start >= end)
return;
int key = start;
for (int i = key + 1; i <= end; i++)
{
if (num[key] > num[i])
{
int b = num[i];
for (int j = i; j > key; j--)
{
num[j] = num[j - 1];
}
num[key] = b;
key++;
}
}
QuickSort(num, start, key - 1);
QuickSort(num, key + 1, end);
}

原文地址:https://www.cnblogs.com/xiaobao2017/p/9673280.html