看我这篇,部分快速排序,有代码
http://www.cnblogs.com/eaglet/archive/2009/05/01/1447481.html
献丑了,写了个代码,这里代码只是用了15个元素作了个demo,10万个数据也一样的道理。代码如下
class Program
{
static void Main(string[] args)
{
int[] arr = { 1, 32, 43, 54, 2, 3, 5, 6, 4, 2, 7, 8, 9, 9, 0 };
QuickSort(arr, 0, arr.Length - 1);
int[] result = new int[5];
int j = 0;
for (int i = arr.Length - 5; i < arr.Length; i++)
{
result.SetValue(arr[i], j);
j++;
}
}
public static void QuickSort(int[] array, int left, int right)
{
if (right - left <= 0) return;
int pivot = array[right];
int temp;
int leftPartition = left - 1;
int rightPartition = right;
while (true)
{
while (array[++leftPartition] < pivot) { }
while (rightPartition > 0 && array[--rightPartition] > pivot) { };
if (leftPartition >= rightPartition) break;
//元素交换
temp = array[leftPartition];
array[leftPartition] = array[rightPartition];
array[rightPartition] = temp;
}
//元素交换
temp = array[leftPartition];
array[leftPartition] = array[right];
array[right] = temp;
//递归调用部分
QuickSort(array, left, leftPartition - 1);
QuickSort(array, leftPartition + 1, right);
}
}
}
这个代码应该就能用的