我有一个数组创建10个随机整数,然后使用quicksort排序它们..我的问题是,当我改变这个创建1,000,000随机整数它不会这样做...可以帮助吗?使用快速排序排序数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RepeatAssignmentQ2
{
class Program
{
static public int Partition(int[] myArray, int left, int right)
{
int pivot = myArray[left];
while (true)
{
while (myArray[left] < pivot)
left++;
while (myArray[right] > pivot)
right--;
if (left < right)
{
int temp = myArray[right];
myArray[right] = myArray[left];
myArray[left] = temp;
}
else
{
return right;
}
}
}
static public void QuickSort_Recursive(int[] arr, int left, int right)
{
// For Recusrion
if (left < right)
{
int pivot = Partition(arr, left, right);
if (pivot > 1)
QuickSort_Recursive(arr, left, pivot - 1);
if (pivot + 1 < right)
QuickSort_Recursive(arr, pivot + 1, right);
}
}
static void Main(string[] args)
{
Random rnd = new Random();
DateTime startTime = DateTime.Now;
int ind = 0;
int length = 1000000;
int[] myArray = new int[length];
while (ind < 1000000)
{
myArray[ind] = rnd.Next(1000000);
ind++;
}
int lengthTwo = 10;
Console.WriteLine("QuickSort by recursive method");
QuickSort_Recursive(myArray,0, lengthTwo - 1);
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine(myArray[i]);
}
Console.WriteLine("Total Time: {0}\n", DateTime.Now - startTime);
Console.WriteLine();
}
}
}
由于
编辑 - 当我囤程序具有10个编号的数组它将显示它们,并让它们排序。当我将其更改为1,000,000并运行程序时,不显示任何内容。
编辑2 - 好吧,出于某种奇怪的原因它正在这样做。我改变了上面的代码来显示更改,它现在显示它随机生成的数字,但没有对它们进行排序。但是当IT人员只需要创建10个随机数就可以对其进行排序。
['Array.Sort'](http://msdn.microsoft.com/en-us/library/system.array.sort.aspx)使用快速排序interally。 – Romoku
“它不会这样做”有点含糊。请具体说明您的问题。 –
什么是你的问题通过这个文件,它一定会给你清晰的图片http://vinayakgarg.wordpress.com/2011/10/25/time-comparison-of-quick-sort-insertion-sort-and-bubble -sort/ –