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


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

    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();

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

    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();

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

  • 相关阅读:
    MSSQL数据库 事务隔离级别
    CSS(Cascading Style Shee)
    Winform MD5
    Winform 基础
    ASP.NET 设置DropDownList的当前选项
    如何彻底关闭退出vmware虚拟机
    Winform GDI+
    SQL优化
    登录
    Spring AOP的应用
  • 原文地址:https://www.cnblogs.com/warrior/p/3678682.html
Copyright © 2020-2023  润新知