Find the i-th smallest number of an array A containing n integers

Problem: Find the i-th smallest number of an array A containing n integers.

 

Solution 1: Sort

Sort the array A from small to large, and then the ith number is the desired value.

Time complexity is O (n²)

 

Solution 2: Maintain an ordered array B [i] of length i (the order is from small to large), traverse A [j], and traverse each element to the largest element in B (ie the last element in B ) For comparison, if it is larger than the largest element in B, continue to traverse. Otherwise, the largest element is squeezed out and inserted into the appropriate position in B, which is similar to the in-line method.

      For example, i = 3, the process is as follows:

8

12

9

1

24

2

20

17

3

15

The former first three elements of the array B are filled in ascending order, and three elements fill Number

8

9

12

      j to traverse the other elements in A, at this time to the fourth element A [j] = 1,

     Because 1 is smaller than 12, 1 squeezes out 12 and successfully ranks among the three smallest elements at present. Then, according to the direct interpolation method, it is compared with the first two elements in sequence and inserted into the correct position. The results are as follows:

1

8

9

     When j traverses to the fifth element A [j] = 24, because 24 is larger than the largest element 9 in B, so it cannot enter B, and j continues to traverse backwards:

1

8

9

    When j traverses to the sixth element A [j] = 2, the same reason, because 2 is smaller than 9, 9 is squeezed out, 2 is inserted into the appropriate position in B

1

2

8

    By analogy, the final result of B is:

1

2

3

At this time, the largest element in B is the desired value. In this example, 3 is the third smallest number in the array A.

Time complexity: In the worst case, each element must be compared with the number of i maintained, so the time complexity is O (ni), but if the value of i is relatively large, think of a limit case i close to n, maybe It will degenerate into O (n²), and its effect is not as good as the direct sorting of solution one (O (nlog (n)).

 

Solution 3: The cost of maintaining an array is too great. We can use the heap data structure instead, which is similar to the solution 2.

Maintain a large top heap with node number i (each parent node is greater than or equal to its child node), available by nature, this large top heap stores the current i-smallest element, the top element of the heap is this most The maximum value of the i small element, which is the i-th small element, is also sought.

The specific steps are:

(1) According to the first i elements of the original array, first create a large top heap with node number i.

(2) Traverse the original array in sequence. Similarly, if the element is greater than or equal to the top element of the pile, continue traversing; otherwise replace the top element of the pile with this element and readjust it to the large top pile according to the sinking operation.

(3) When the original array traversal is completed, the top element of the heap at this time is all, the i-th smallest element.

Complexity analysis: the time complexity of creating the heap is O (i), the time complexity of traversing the remaining arrays is O (ni), and the time complexity of readjusting the heap each time is O (logi), and it must be readjusted every traversal Heap, so the total time complexity is O ((ni) logi + i).

 

Solution 4: Divide and conquer

Similar to fast sorting, when a quick sorting is completed, the left side of the hub element is all less than or equal to the element, and the right side of the hub element is all greater than the element.

For example, if there are k numbers to the left of the hub element, the hub element is the k + 1th smallest number.

Then compare the size of k + 1 and i,

If k + 1 <i, go to the right of the pivot element and continue to divide and conquer. At this time, we are looking for the [i- (k + 1)] th element, because the left side has already offset k + 1.

If k + 1> i, then go to the left of the pivot element to continue to divide and conquer, and at this time it is still looking for the i-th element.

If k + 1 = i, the hub element is the i-th smallest element.

Note: This question can also be extended to find the ith largest number, which is very similar to finding the ith decimal number. Here only illustrates the basic idea of ​​the algorithm, code readers can be interested to see

Published 61 original articles · 61 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_42475914/article/details/104744006