从十亿个数中找出前1000个最大的数的算法

    10亿个数中找出1000个最大的数的算法思路:

   1,先拿出前1000个数字,并排序,找出最小值为 minValue .
   2,  然后再依次拿出1000个数字,找出最大值为 tempMaxValue.
   3,  如果tempMaxValue 比 minValue 大, 则将 theMaxValue 放入前1000个数中,再排序并找出minValue .
   4 .. 依此类推。 即可得到1000个最大的数。 


   以下是本人测试的代码 。可以参考,欢迎高手指正,谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace test
{
    public class Class1
    {
        private  const int MaxLength = 100000000;
        private const int EachCount = 1000;

        public static void TestDirectSort()
        {
            Console.WriteLine(" test start :" + DateTime.Now.ToLongTimeString());

            int[] orignList = new int[MaxLength];

            Random rd = new Random();
            for (int i = 0; i < MaxLength; i++)
            {
                orignList[i] = rd.Next();
            }

            ArrayList list = new ArrayList(MaxLength);
            list.AddRange(orignList);
            list.Sort();

            int[] retList = new int[1000];
            int j = 0;
            for (int i = MaxLength - 1; i > MaxLength - 1000; i++)
            {
                retList[j] = orignList[i];
                j++;
            }

            Console.WriteLine(" test end : " + DateTime.Now.ToLongTimeString());

            foreach (int i in list)
            {
                Console.WriteLine(i.ToString());
            }
            Console.WriteLine("end: " + DateTime.Now.ToLongTimeString());

        }

        public static void TestSort()
        {
            Console.WriteLine(" test start :"  + DateTime.Now.ToLongTimeString());

            int[] orignList = new int[MaxLength] ;
           
            Random rd = new Random();
            for (int i = 0; i < MaxLength; i++)
            {
                orignList[i] = rd.Next();
            }

            ArrayList foreList = new ArrayList();
            if (MaxLength > EachCount)
            {
                for (int i = 0; i < EachCount; i++)
                {
                    foreList.Add(orignList[i]);
                }

                foreList.Sort();
                int addValue = EachCount;
                for (int j = 1; j < MaxLength / EachCount +1 ; j++)
                {
                    ArrayList tempList = new ArrayList();
                    for (int k = j * addValue; k < j * addValue + EachCount; k++)
                    {
                        if (k >= MaxLength) break;
                        //tempList[k - j * addValue] = orignList[k];
                        tempList.Add(orignList[k]);
                    }

                    tempList.Sort();
                    for (int m = tempList.Count -1; m >=0 ; m--)
                    {
                        if ((int)tempList[m] <= (int)foreList[0])
                            break;
                       
                        foreList[0] = tempList[m];
                        foreList.Sort();
                    }
                }
            }
            else
            {
                foreList.AddRange(orignList);
                foreList.Sort();
            }

            Console.WriteLine(" test end : " + DateTime.Now.ToLongTimeString());

            foreach (int i in foreList)
            {
                Console.WriteLine(i.ToString());
            }
            Console.WriteLine("end: " + DateTime.Now.ToLongTimeString());
        }
    }

    public class client
    {
        public static void Main()
        {
            Class1.TestSort();

           // Class1.TestDirectSort();

            Console.Read();

        }
    }
}

原文地址:https://www.cnblogs.com/relang99/p/1202073.html