• 发表于 3年前
• 阅读 5
• 收藏 0
• 评论 0

# 传统的双重循环查找也是可取的，但效率实在不敢恭维，特别是处理大量数据时候

``````class Program { static void Main(string[] args) { //产生随机数
int[] array = Enumerable.Range(1, 100000).OrderBy (n => Guid.NewGuid()).Take(80000).ToArray(); DateTime dt1 = DateTime.Now; int max = array[0]; int flag; //数组无序排列，查找最大值
for (int i = 1; i < array.Length; i++) { if (array[i] > max) { max = array[i]; } } for (int i = 1; i <= max; i++) { flag = 1; for (int j = 0; j < array.Length; j++) { //相等标记Flag=0,意味着不是缺少的数字
if (i.Equals(array[j])) { flag = 0; break; } } if (flag == 1) { Console.Write("{0},", i); } } DateTime dt2 = DateTime.Now; TimeSpan ts = dt2 - dt1; Console.WriteLine("\r\n" + "共耗时间{0}ms", ts.TotalMilliseconds);//52730.5525

# 位图法测试

``````class Program { static void Main(string[] args) { //随即产生80000个不重复数
int[] array = Enumerable.Range(1, 100000).OrderBy (n => Guid.NewGuid()).Take(80000).ToArray(); //int[] array={1,2,3,5,7,9,10,12,45,62,55,78,98,52,12,4,200,60,63,65,66,67,68,69,70,74,79,80,82,89,90,91,92,93,94,98,100,101};
DateTime dt1=DateTime.Now; //找出最大值
int max=array[0]; for (int i = 1; i < array.Length; i++) { if (array[i]>max) { max = array[i]; } } //新数组的长度为旧数组最大数字+1
int[] lose=new int[max+1]; foreach (int item in array) { //若Item为2,则Lose[2]=1...所以新数组的长度为旧数组最大数字+1
lose[item] = 1; } //那么为0的就是缺少值
for (int i = 1; i < lose.Length; i++)//100
{ if (lose[i].Equals(0)) { Console.Write("{0},",i); } } DateTime dt2=DateTime.Now; Console.WriteLine("\r\n"+(dt2-dt1).TotalMilliseconds);//6004.3379Ms

×