生成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条记录