zoukankan      html  css  js  c++  java
  • Selection in expected linear time

    下面的是我用C#写的一个算法, 功能是从一个数组中选择第 i 小的一个数, 平均时间复杂度是Θ(n).

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Diagnostics;

     

    class Program

    {

        static void Main(string[] args)

        {

            int[] array = new int[] { 9, 3, 4, 7, 10, 5, 1 };

            int ismall = RandomizedSelect(array, 3);

            Debug.Assert(ismall == 4);

        }

     

        /// <summary>

        /// select the value of the i-th smallest number of the array

        /// </summary>

        static int RandomizedSelect(int[] array, int i)

        {

            return RandomizedSelect(array, 0, array.Length - 1, i);

        }

     

        /// <summary>

        /// select the value of the i-th smallest number between array[startIndex] and array[endIndex]

        /// </summary>

        static int RandomizedSelect(int[] array, int startIndex, int endIndex, int i)

        {

            if (startIndex == endIndex)

                return array[startIndex];

            int q = Partition(array, startIndex, endIndex);

            int k = q - startIndex + 1;

            if (k == i)

                return array[q];

            else if (k < i)

                return RandomizedSelect(array, q + 1, endIndex, i - k);

            else

                return RandomizedSelect(array, startIndex, q - 1, i);

        }

     

        /// <summary>

        /// partition the array(smaller left, bigger right), return the pivot index

        /// </summary>

        static int Partition(int[] array, int startIndex, int endIndex)

        {

            //just using the middle number, thought it may not be the best way

            int mid = (endIndex + startIndex) / 2;

            int v = array[mid];

            Swap(array, mid, endIndex);

            int i = startIndex - 1;

            int j = endIndex;

            while (true)

            {

                while (array[++i] < v) { }

                while (array[--j] > v) { }

                if (i > j)

                    break;

                Swap(array, i, j);

            }

            Swap(array, i, endIndex);

            return i;

        }

     

        /// <summary>

        /// swap two numbers in the array

        /// </summary>

        static void Swap(int[] array, int index1, int index2)

        {

            int temp = array[index1];

            array[index1] = array[index2];

            array[index2] = temp;

        }

    }

    最近看<<算法导论>>, 虽然是英文版的, 但也比我买的一本<<算法基础>>好懂, 那本<<算法基础>>翻译的实在是让人感觉不知所云.
  • 相关阅读:
    智能电视可以安装软件就可以摆脱很多限制,而且可以和PC共享影音资源这个很靠谱。
    【转载】福昕PDF电子文档处理套件 企业版 注册码 注册方法
    OS X系列文章 AirPlay+Apple TV影音方案研究[转]
    SHARP 316L打印机64位驱动问题
    我和电脑的二三事
    北信源DeviceRegister.exe的卸载方法 【转】
    上篇随笔的补充。
    ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c
    WPF中类似使用tab键功能,可以向上向下定位
    c#通过datatable导出excel和word
  • 原文地址:https://www.cnblogs.com/Dah/p/552185.html
Copyright © 2011-2022 走看看