[Introduction to algorithms-015] Selection in expected Linear Time)

Source: Internet
Author: User
1. algorithm concept Problem description: Find the I-th small element from the array (where there are no repeated elements in the array ), this is a classic question about "selection in expected linear time. Idea: Introduction to algorithms 215 page 9.2 Selection in each CT linear time2. Java implementation idea: Introduction to algorithms 216 page pseudocode

/* The algorithm is expected to be used for Linear Time Selection. input requirements: No repeated elements in array */public static int randomizedselect (INT [] array, int start, int end, int I) {If (START = END) {return array [start];} int middle = randomizedpartition (array, start, end); int K = middle-start + 1; if (I = k) {return array [Middle];} else if (I <k) {return randomizedselect (array, start, middle-1, I );} else {return randomizedselect (array, middle + 1, end, I-k );}}
Randomizedpartition (array, start, end) See [Introduction to algorithms-010] Quick Sort)

3. Non-recursive version

/* Linear Time Selection for non-recursive versions */public static int iterativerandomizedselect (INT [] array, int start, int end, int I) {int result = 0; while (start <= END) {If (START = END) {return array [start];} int middle = randomizedpartition (array, start, end ); int K = middle-start + 1; if (I = k) {result = array [I];} else if (I <k) {end = middle-1 ;} else {start = middle + 1; I = I-K ;}} return result ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.