File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33 *
44 * This program finds the smallest 'k' numbers from a given list of numbers.
55 * It demonstrates three modern C++ implementations:
6- *
7- * 1. Simple Sort Method:
8- * - Sorts the entire list and then takes the first k numbers.
9- * - Time Complexity: O(n log n)
10- *
11- * 2. Max Heap (Priority Queue) Method:
12- * - Maintains a max heap of size k, inserting numbers and removing the
13- * largest when the heap exceeds size k.
14- * - Time Complexity: O(n log k)
15- *
16- * 3. nth_element Method:
17- * - Uses std::nth_element to partition the vector so that the first k
18- * elements are the smallest, then sorts those k numbers.
19- * - Average Time Complexity: O(n)
20- *
216 * Edge Cases:
227 * - If k is 0, an empty vector is returned.
238 * - If k is greater than or equal to the input size, the entire sorted list is
You can’t perform that action at this time.
0 commit comments