这个应该是程序员必备的算法吧。。。感觉几百个数冒泡就有点费劲了的说。N方跟NlogN的差距,数据量越大越明显。
因为水平有限,就整的递归的快排,非递归的。。。原来有个朋友写过,不知道能不能找到,找到了就翻译成C#版的扔出来
还有一点,因为最早学快排是在学Pascal的时候学的,所以后来用就是直接翻译过来的-_-||
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QSortConsole
{
class Program
{
static void Main(string[] args)
{
int[] a = { 9, 1, 5, 4, 3 };
QuickSort qsort = new QuickSort(a);
qsort.GetResult();
foreach (int i in a)
Console.WriteLine(i);
}
}
class QuickSort
{
private int[] num;
public QuickSort(int[] num)
{
this.num = num;
}
private void QSort(int l, int r)
{
int i, j, k, x;
x = num[(l + r) / 2];
i = l;
j = r;
do
{
while (x > num[i]) i++;
while (x < num[j]) j--;
if (i <= j)
{
k = num[i];
num[i] = num[j];
num[j] = k;
i++;
j--;
}
} while (i <= j);
if (l < j) QSort(l, j);
if (i < r) QSort(i, r);
}
public int[] GetResult()
{
QSort(0, num.Length-1);
return num;
}
}
}