using System;
namespace test1
{
class Program
{
/*
IComparable接口提供CompareTo方法
ref 可以去掉
*/
public static void Quicksort<T>(ref T [] a,int low,int high) where T:System.IComparable<T>
{
if(low>=high)
{
return;
}
int i = low, j = high + 1;
T key = a[low];
while(true)
{
while (a[++i].CompareTo(key)<0 && i < high) ; //a.CompareTo(b)>0 即 a>b
while (a[--j].CompareTo(key)>0) ;
if (i >= j)
break;
T tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
a[low] = a[j];
a[j] = key;
Quicksort(ref a, low, j - 1);
Quicksort(ref a, j + 1, high);
}
static void Main(string[] args)
{
int[] a = { 23, 412, 42, 45, 67, 3, 6, 34, 56 };
Quicksort(ref a, 0, a.Length - 1);
foreach(int element in a)
{
Console.WriteLine(element);
}
}
}
}
近期评论