首页 > 其他 > 详细

随笔,关于生成随机数,排序

时间:2014-04-21 20:44:11      阅读:538      评论:0      收藏:0      [点我收藏+]

生成N个随机数,N<100W,不能重复

bubuko.com,布布扣
Console.WriteLine(DateTime.Now.ToLongTimeString());
Random random = new Random();
Byte[] bytes = new Byte[1000000];
StreamWriter sw = new StreamWriter("Values.txt", true);
for (int i = 0; i < 1000000; i++){
    var result = random.Next(0, 1000000);
    if(bytes[result] == 1) 
    {
        continue;
    }
    bytes[i] = 1;
    sw.WriteLine(result);
    sw.Flush();
}
sw.Close();
Console.WriteLine(DateTime.Now.ToLongTimeString());
Console.Read();
bubuko.com,布布扣

接上题,对N个随机数进行排序,N<100W

bubuko.com,布布扣
StreamReader sr = new StreamReader("Values.txt");
Byte[] bytes = new Byte[1000000];
Console.WriteLine(DateTime.Now.ToLongTimeString());
while (!sr.EndOfStream)
{
    bytes[Int32.Parse(sr.ReadLine())] = 1;
}
sr.Close();

StreamWriter sw = new StreamWriter("Values2.txt", true);
for (int i = 0; i < 1000000; i++)
{
    if(bytes[i] == 1) 
    {
        sw.WriteLine(i);
        sw.Flush();
    }
}
sw.Close();
Console.WriteLine(DateTime.Now.ToLongTimeString());
Console.Read();
bubuko.com,布布扣

整个排序过程耗时2秒,大约70W条记录

随笔,关于生成随机数,排序,布布扣,bubuko.com

随笔,关于生成随机数,排序

原文:http://www.cnblogs.com/warrior/p/3678682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!