class Algorithm
{
int[] array;
int N;
int maxnumber;
public void InitRandom(int size, int maxnumber)
{
array = new int[size];
N = size;
Random gen = new Random();
for (int i = 0; i < size; i++)
{
array[i] = gen.Next(maxnumber);
}
}
public void InitAscending(int size)
{
array = new int[size];
N = size;
for (int i = 0; i < size; i++)
{
array[i] = i;
}
}
public void InitDescending(int size)
{
array = new int[size];
N = size;
for (int i = 0; i < size; i++)
{
array[i] = size - i;
}
}
这是它的主要排序
public void QuickSort(ref long counter)
{
Quick(0, N - 1, ref counter);
}
public void Quick(int left, int right, ref long counter)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = array[left];
while (left < right)
{
while ((array[right] >= pivot) && (left < right))
{
right--;
}
if (left != right)
{
array[left] = array[right];
left++;
}
while ((array[left] <= pivot) && (left < right))
{
left++;
}
if (left != right)
{
array[right] = array[left];
right--;
}
counter++;
}
array[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
{
Quick(left, pivot - 1, ref counter);
}
if (right > pivot)
{
Quick(pivot + 1, right, ref counter);
}
}
最后我设置了这些值并调用方法。我确实知道那里的问题因为我对所有其他排序算法使用了相同的方法
Algorithm a = new Algorithm();
a.InitRandom(10000, 10000);
DateTime starttime = DateTime.Now;
long counter = 0;
a.QuickSort(ref counter);
DateTime endtime = DateTime.Now;
TimeSpan time = endtime - starttime;
double ms = time.TotalMilliseconds;
Console.WriteLine("QuickSort operationcount: " + counter + " time: " + ms);
与InitAscending和InitDescending相同