using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Sorting_Algorithm_Test1
{
class Program
{
private int[] a;
private int[] b;
private DateTime startTime;
private DateTime stopTime;
private bool running = false;
int u = 9999; int l = 1;
private int maxNum = 9999;
public void B()
{
b = new int[65536];
}
public void GetRandomArray(int n)
{
this.a = new int[n];
Random rd = new Random();
for (int i = 0; i < n - 1; i++)
a[i] = rd.Next(maxNum);
//备份生成的ARRAY
B();
Array.Copy(a, b, n);
}
........
//Algorithm1 SelectionSort(A[0…n-1])
public void SortArray(int n)
{
...............
}
}
//Algorithm2 DistributionCounting(A[0..n-1],l,u)
//Sorts an array of integers from a limited range by distribution counting
//Input: An array A [0...n-1] of intergers between l and u(l<=u),Here I define l=1,u=9999;
//Output:Array S[0...n-1] of A’s elements sorted in nondecreasing order
//Implementation of Distribution Counting.
public void SortArray2(int range)
{
int[] S = new int[range];
int[] D = new int[u - l + 1];
for (int i = 0; i <= u - l; i++) D[i] = 0;
// sort
for (int i = 0; i < range; i++) D[this.b[i]]++; //统计频率
for (int i = u - l - 1; i >= 1; i--) D[i] += D[i + 1]; //为分布作准备
for (int i = 0; i < range; i++) S[--D[this.b[i]]] = this.b[i];
b = S;
}
//Timer
.............
}
class Test
{
static void Main()
{
Program aaa = new Program();
StreamWriter SW;
StreamWriter SW2;
SW = File.CreateText("c:\\file1.txt");
SW2 = File.CreateText("c:\\file2.txt");
for (int n = 100; n < 10001; n = n + 100)
{
aaa.GetRandomArray(n);
aaa.Start();
aaa.SortArray(n);
aaa.Stop();
Console.WriteLine("elapsed time in milliseconds: " + aaa.GetElapsedTime());
//elapsed time in milliseconds
SW.Write(aaa.GetElapsedTime());
//use the backup unsorted array
aaa.Start();
aaa.SortArray2(n);
aaa.Stop();
Console.WriteLine("elapsed time in milliseconds: " + aaa.GetElapsedTime());
SW2.Write(aaa.GetElapsedTime());
}
SW.Close();
SW2.Close();
}
}
}
DEBUG没问题,但就是运行不出来
救急,感激不尽...
PS.完整文件在附件里.
[此贴子已经被作者于2006-10-4 13:12:24编辑过]