9-1 Maximum number of sorted I
Given a set of n elements, we hope to use a comparison-basedAlgorithmTo find the largest I elements in order. Find an algorithm that implements the following methods and has the best progressive and worst running time, and analyze the running time of each method (represented by N and I ).
A) sort the input numbers and list the maximum I numbers.
B) Create a priority queue for the input number and call the EXTRACT-MAX process I times.
C) A sequence statistic algorithm is used to locate the largest element I, divide the input array, and sort the maximum number I.
Analysis and answer:
A)The idea of first sorting the input number and then listing the maximum number of I is the simplest. the time complexity is O (nlgn ).
B)Create a maximum heap for the set containing n elements, the time complexity is round (N), then call I EXTRACT-MAX operation, the time complexity is O (I * lgn ), the total time complexity is O (I * lgn ).
C)Use the sequential statistics algorithm to find the largest element I. the time complexity is random (n), and then divide the input array. the time complexity is random (N). Finally, sort the maximum number I, the time complexity is always (I * LGI), and the total time complexity is always (n + I * LGI ).