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: