以下是我自己嘗試的排序代碼,規模量在10000以內的排序,歡迎拍磚,還請多多指教
使用的是C#代碼
class Program
{
static void Main(string[] args)
{
StopWatch sw1 = new StopWatch();
StopWatch sw2 = new StopWatch();
DealSuperSort ss = new DealSuperSort();
sw1.setStart();
//初始化10000以內的8000個正整數
List<int> listInt = ss.getRandomList(10000, 8000);
sw1.setEnd();
sw2.setStart();
//進行排序
ss.sort1(listInt);
sw2.setEnd();
foreach (int i in listInt)
{
Console.WriteLine(i);
}
Console.WriteLine();
Console.WriteLine(sw1.getTime());
Console.WriteLine(sw2.getTime());
Console.ReadLine();
}
}
//姑且稱爲超級排序
class DealSuperSort
{
public void sort1(List<int> listInt)
{
int count = 10000;
int num = 0;
List<bool> listBool = new List<bool>(count+1);//listBool的0位我不要了
for (int i = 0; i <= count; i++)
{
bool b = false;
listBool.Add(b);
}
foreach (int i in listInt)
{
listBool[i] = true;
}
for (int j = 1; j <= count; j++)
{
if (listBool[j] == true)
{
listInt[num] = j;
num++;
}
}
}
/// <summary>
/// 返回[1,max]中隨機排序的num個max以內的不重複的的正整數;
/// </summary>
public List<int> getRandomList( int max, int num)
{
List<int> listInt;
if (max < num)
{
listInt = null;
}
else
{
listInt = new List<int>();
List<int> container = new List<int>(max);
for (int i = 1; i < max+1; i++)
{
container.Add(i);
}
Random r = new Random();
int index = -1;
int value = -1;
for (int j = 0; j < num; j++)
{
index = r.Next(0, container.Count);
value = container[index];
listInt.Add(value);
container.RemoveAt(index);
}
}
return listInt;
}
}
/// <summary>
/// 秒錶類(借鑑了互聯網)
/// </summary>
public class StopWatch
{
private DateTime DateTime1, DateTime2;
private string dateDiff = null;
public void setStart()
{
DateTime1 = DateTime.Now;
}
public void setEnd()
{
DateTime2 = DateTime.Now;
}
public DateTime getStart()
{
return DateTime1;
}
public DateTime getEnd()
{
return DateTime2;
}
public string getTime()
{
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
//顯示時間
dateDiff = ts.Days.ToString()+ "天"
+ ts.Hours.ToString() + "小時"
+ ts.Minutes.ToString() + "分鐘"
+ ts.Seconds.ToString() + "秒"
+ ts.Milliseconds.ToString() + "毫秒";
return dateDiff;
}
}
規模量 初始化時間 排序時間
10000 31ms 0ms
100000 2s343ms 0ms
1000000 6min58s921ms 78ms
以下是我還沒有弄明白的
1:有沒有比ms更小的時間計量單位了。。。在這裏貌似沒有了。。
但是我覺得可以自己手動製作一個時間計量單位,但是要用到多線程
2:排序時間真的有這麼快嗎....初始化時間是不是應該還可以優化
3:初始化時間的增長速度遠高於規模量的增加速度