C language for linear expected time Selection

Source: Internet
Author: User

In other words, the question of selecting the nth minimum is previously feared. I have read this article today. <ata> I have benefited a lot from this issue. although I have been writing a simple selection question for an hour, I only know if I really understand it when I implement the code. moreover, this item cannot be written in a sentence in the memory area.

The idea is to constantly find an exact position in the current array, to know how many elements before the current position are less than or equal to the value at the position, and then compare the position with the target position. narrow down the scope to achieve the goal.

In terms of running time, It is θ (n). The analysis process is very math and I will not make an axe.

Stick it up. My head feels a bit dull.

/* 2011-05-02-19.w.c -- expected Linear Time Selection */<br/> # include <stdio. h> </P> <p> # define size (20) </P> <p> int main (void); <br/> int select (int * const array, const int left, const int right, const int ith); <br/> int partition (int * const array, const int left, const int right ); <br/> void swap (int * const PLV, int * const PRV); </P> <p> int main (void) <br/> {<br/> int array [size] = {5, 6, 7, 9, 8, 4, 3, 2, 1, 10, 12, 11, 13, 14, 15, 16, 17, 19, 18, 20 }; <br/> int size = size; <br/> int ith = 19; </P> <p> printf ("the % d % s least number is: % d/N", ith, ith> 3? "Th": (1 = ith? "St": (2 = ith? "Nd": "RD"), select (array, 0, size-1, ith); </P> <p> return 0; <br/>}</P> <p> int select (int * const array, const int left, const int right, const int ith) <br/>{< br/> int trim, KTH; </P> <p> If (Left = right) <br/> return array [left]; <br/> partition = partition (array, left, right); <br/> kth = trim-left + 1; <br/> If (ith = kth) <br/> return array [th]; <br/> else if (ith <KTH) <br/> return Select (array, left, lower-1, ith); <br/> else <br/> return select (array, lower + 1, right, ith-KTH ); <br/>}</P> <p> int partition (int * const array, const int left, const int right) <br/>{< br/> int partition, i, J; </P> <p> align = right; <br/> I = left; <br/> J = right-1; <br/> while (1) <br/> {<br/> while (I! = Right & array [I] <array [distinct]) <br/> I ++; <br/> while (J! = Left & array [J]> = array [distinct]) <br/> j --; <br/> if (I <j) <br/> swap (array + I, array + J); <br/> else <br/> break; <br/>}< br/> swap (array + I, array + callback); </P> <p> return I; <br/>}</P> <p> void swap (int * const PLV, int * const PRV) <br/>{< br/> int temp; </P> <p> temp = * PLV; <br/> * PLV = * PRV; <br/> * PRV = temp; <br/>}

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.