Practical Algorithm analysis and programming chapt 2 sequential statistics algorithm and Median

Source: Internet
Author: User
ArticleDirectory
    • 2.1 Algorithm for sequential statistics (Algorithm for Finding small elements I)

In N element sets, the I-th sequence is used for statistics, that is, the I-th element.

Median, that is, the nth/2th small element.

2.1 Ordered Statistics Algorithm (Algorithm for Finding small elements I)

You can use the division and control method. Refer to the quick sorting method:

Set the I-th small element to S

Divide:

A [0... n]

Central region = A [0]

Divide the set into a [0... j], a [J + 1... n], so that all elements in the previous set are less than limit, and all elements in the next set are greater than limit

Conquer:

If (S <= substring), Recursively search in range 1

If (S> recursive), recursive search in range 2

Combine:

None

# Include <iostream> Int Split ( Int Arr [],Int Lo, Int Hi ){ Int Role = arr [lo]; Int Pos = lo; For ( Int I = lo + 1; I <= Hi; ++ I ){ If (ARR [I] <strong & Pos <I) {++ Pos; STD: swap (ARR [POS], arr [I]) ;}} STD :: swap (ARR [lo], arr [POS]); Return Pos ;} Int Find_ith ( Int Arr [],Int Lo, Int Hi, Int K ){ Int Mid; If (Lo  If (K <= mid) {find_ith (ARR, lo, mid, k );} Else   If (K> mid) {find_ith (ARR, Mid + 1, hi, k );}} Return Arr [k];} Int Main (){ Int Arr [10] = {10, 20, 34, 4, 14, 8, 7, 73, 54, 5 }; For ( Int I = 0; I <10; ++ I) {STD: cout <" The "<I + 1 <" Th small element is: "<Find_ith (ARR, 0, 9, I) <" \ N ";} System (" Pause "); Return 0 ;}

Algorithm complexity analysis: O (nlogn)

Find an algorithm with linear time complexity for small I elements. For details, see algorithm design skills and analysis P110.

 

Application of the median of 2.2

 

 

 

 

 

 

 

[Proof]

 

[Example] optimal location of Oil Pipes
Professor Olay is consulting an oil company that plans to build a pipeline from east to west. The rolling pipeline will go through an oil field with N wells. Each well has a tubing along the shortest path directly connected to the main pipe (or south or north ).

 

 

 

 

 

 

 

Determine the X and Y coordinates of each well. How can Professor Olay choose the optimal position of the Main Pipe (even if the sum of the length of each nozzle is the smallest ?)

[Analysis] determining the optimal position of the upper pipeline is actually finding the median of Y coordinates of N wells;

 

Application of weighted median:

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.