public static int randomSelect(int[] A, int k)
        {
            return randomSelectDo(A, 0, A.Length - 1, k);
        }         private static int randomSelectDo(int[] A, int low, int high, int k)
        {
            int i = randomPartition(A, low, high);
            //n is the number of < A[i]
            int n = i - low;
            if (n > k)
                return randomSelectDo(A, low, i - 1, k);
            else if (n == k)
                return A[i];
            else
                return randomSelectDo(A, i + 1, high, k - n - 1);
        }         private static void swap(int[] A, int i, int j)
        {
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }         private static int randomPartition(int[] A, int low, int high)
        {
            //random divide
            Random rand = new Random();
            int r = rand.Next(high - low + 1) + low;
            swap(A, low, r);
            int i = low;
            int x = A[low];
            for (int j = low + 1; j <= high; j++)
            {
                if (A[j] < x)
                {
                    i++;
                    if (i != j)
                    {
                        swap(A, i, j);
                    }
                }
            }
            swap(A, low, i);
            return i;
        }
        static void Main(string[] args)
        {
            int[] I = new int[10];
            Random r = new Random();
            B:
            for (int i = 0; i < I.Length; i++)
            {
                I[i] = r.Next(20);
            }             foreach (int i in I)
            {
                Console.Write(i+" ");
            }
            Console.WriteLine("_______________________________________");            int t= randomSelect(I, 0);
           Console.WriteLine(t);
           if (Console.ReadLine() == "a")
           {
               goto B;
           }
        }

最新文章

  1. 115个Java面试题和答案——终极列表(下)
  2. DirectoryInfo类
  3. Linux下统计出现次数最多的指定字段值
  4. POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3
  5. 使用WIF实现单点登录Part II —— Windows Identity Foundation基本原理
  6. C/C++:类模板
  7. floor() 和 ceil()函数
  8. Handler处理长时间事件
  9. Xcode8插件安装
  10. uCOS-II
  11. 【PMP】易混淆知识点
  12. xdg-open命令智能打开各种格式的文件
  13. HttpClient的POST请求返回302解决
  14. 计算机&amp;通信词典
  15. wordpress WP-PageNavi分页
  16. 通过微信Android和iOS版,看两大系统的差异
  17. c++浅拷贝与深拷贝(LeetCode669)
  18. linux 分卷压缩和合并
  19. pycharm控制台出现python编译器的编辑功能
  20. CodeChef SADPAIRS:Chef and Sad Pairs

热门文章

  1. .NET系统开发过程中积累的扩展方法
  2. DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表
  3. 3 sum
  4. 数组排序、递归——(Java学习笔记二)
  5. 汉字转拼音 oracle方式 [转]
  6. zoj 3210 A Stack or A Queue? (数据结构水题)
  7. css优先级计算规则
  8. 脉冲神经网络Spiking neural network
  9. Iframe父页面与子页面之间的调用
  10. 用windows性能监视器监控sqlserver的常见指标