排序方法,思想都不一样 我写的快排
public static void QuickSort(int[] array, int left, int right)
{
if (left < right)
{
int middle = GetMiddleFroQuickSort(array, left, right);
QuickSort(array, left, middle - 1);
QuickSort(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(int[] array, int left, int right)
{
int temp = array[left];
while (left < right)
{
while (left < right && array[right]>temp)
{
right--;
}
if (left < right)
{
int tm = array[left];
array[left] = array[right];
array[right] = tm;
left++;
}
while (left < right && array[left] < temp)
{
left++;
}
if (left < right)
{
int tm = array[right];
array[right] = array[left];
array[left] = temp;
right--;
}
array[left] = temp;
}
return left;
}
static void Main(string[] args)
{
int[] array = new int[] { 12, 5, 8, 20, 97, 3 };
Quick.QuickSort(array,0,array.Length-1);
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadKey();
} |