以下是我自己尝试的排序代码,规模量在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:初始化时间的增长速度远高于规模量的增加速度
本文分享了一种在C#中实现的大规模数据排序方法,该方法能够在短时间内完成上百万条记录的排序任务。通过实验对比,展示了其在不同数据规模下的初始化时间和排序时间,并探讨了进一步优化的可能性。
533

被折叠的 条评论
为什么被折叠?



