主题:一个作业,关于SORTING效率比较,哪为大哥来看看....我实在想不出来错哪里,谢谢
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)
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
public void Start()
{
this.startTime = DateTime.Now;
this.running = true;
}
public void Stop()
{
this.stopTime = DateTime.Now;
this.running = false;
}
//Elaspsed time in milliseconds
public double GetElapsedTime()
{
..... }
}
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没问题,但就是运行不出来
救急,感激不尽...
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)
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
public void Start()
{
this.startTime = DateTime.Now;
this.running = true;
}
public void Stop()
{
this.stopTime = DateTime.Now;
this.running = false;
}
//Elaspsed time in milliseconds
public double GetElapsedTime()
{
..... }
}
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没问题,但就是运行不出来
救急,感激不尽...