In a set composed of n elements, order statistic is the smallest element in the set. For example, in a set composed of n elements, the minimum value is 1st sequence statistics, and the maximum value is the nth sequence statistics. The median always appears at low (n + 1)/2) or high (n + 1)/2, here, low is the downward round ("lower median"), and high is the upward round ("upper median"). When n is an odd number, there is only the "lower median ", when N is an even number, there are both "lower median" and "upper median ".
The problem is defined as follows.
Input: a set of n different numbers a and I. I belong to the range [1, N].
Output: X, an element in set a, is exactly greater than other I-1 elements in
The sorting method can solve this problem, such as heap sorting and merge sorting. Time can reach O (N * lg (n ))
The following describes a practical algorithm. The average running time is O (n)
This program uses the fast sorting partition subprogram (randomly select the shard version), because partition always divides smaller than the Shard to the left and greater than the Shard to the right, so use this (but randomizedselect does not recursively process the two sides of the partition like the quick sort, but only processes the left or right side, so it is faster) to complete the selection.
The average performance of this algorithm is better, because it is a randomization Division, there is no specific set of input, resulting in its worst case.
The average time is O (n), and the worst time is O (n ^ 2)
The implementation code is as follows:
1 package algorithms; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 public class SelectionProblem { 6 7 //static StringBuilder logger = new StringBuilder(); // debug 8 //static String NEWLINE = "\n"; // debug 9 10 /**11 * @param a the array12 * @param low the lower bound (inclusive)13 * @param high the upper bound (exclusive)14 * @param i indicate that the i-th order statistic is our target, i starts from 115 * @return the i-th order statistic16 * */17 public static <T extends Comparable<T>>18 T randomizedSelect(T[] a, int low, int high, int i) {19 --high; // high the upper bound (exclusive)20 return _randomizedSelect(a, low, high, i);21 }22 23 private static <T extends Comparable<T>>24 T _randomizedSelect(T[] a, int low, int high, int i) {25 if (low == high) {26 return a[low]; // target found27 }28 // else, partition29 int pivot = randomizedPartition(a, low, high);30 int k = pivot - low + 1;31 if (k == i) { // if pivot is our target32 return a[pivot];33 } else if (k > i) { // if pivot is too large34 return _randomizedSelect(a, low, pivot-1, i);35 } else { // if pivot is too small36 return _randomizedSelect(a, pivot+1, high, i-k);37 }38 }39 40 private static <T extends Comparable<T>>41 int randomizedPartition(T[] a, int low, int high) {42 int pivotIndex = randomIndex(low, high+1);43 // logger.append("pivotIndex:"+pivotIndex+NEWLINE); // debug44 return Partition.doPartition(a, low, high+1, pivotIndex);45 }46 47 private static final Random random = new Random();48 // low (inclusive), high (exclusive)49 private static int randomIndex(int low, int high) {50 if (high==low) {51 return low;52 }53 return random.nextInt(high-low) + low;54 }55 56 // test57 public static void main(String[] args) {58 Integer[] a = new Integer[]{29, 36, 44, 12, 29, 24, 28, 74, 54, 56};59 System.out.println(Arrays.toString(a));60 Integer result = SelectionProblem.randomizedSelect(a, 0, a.length, 10);61 //if (result != 36) { // debug62 // System.out.println(logger); // debug63 //} // debug64 System.out.println("result:"+result);65 //System.out.println(Arrays.toString(a)); // debug66 QuickSort.sort(a, 0, a.length);67 System.out.println(Arrays.toString(a));68 }69 70 }
Selection Problem (Selection Problem)