Problem 1056. Partial sorting algorithm
Return the top k elements of an input vector. A comparison function compareFcn(m,n) is provided to compare individual elements of the vector. The function returns true is element m should be ranked higher than element n. e.g.
partial_sort([4 3 2 1], 2, @(m,n) m > n)
ans =
4 3
partial_sort([4 3 2 1], 2, @(m,n) m < n)
ans =
1 2
All elements in the input vector will be unique.
Solutions will be ranked on the total number of times comparison functions are called, over all the test cases.
Solution Stats
Problem Comments
-
4 Comments
Argh! The 'mod sorting' has me flummoxed!
Is this broken? On my machine at least the anonymous comparison function is not found in profile('info').
Also, note that in the "mod comparison" case, the ordering has some unintuitive properties. For instance, 30 > 6 since 30 divides 6 and 30 ~ 20 since neither divides the other, but 20 ~ 6 for the same reason, meaning that ~ is not transitive using this comparison function.
A final issue: the comparison the "mod comparison" case does not match the description in the problem. The problem states that this function should return "true [if] element m should be ranked higher than element n" (m > n), but in this test case, the function also returns true when m is equal to n (and so implements m >= n).
Solution Comments
Show commentsProblem Recent Solvers9
Suggested Problems
-
166 Solvers
-
Find the sum of the elements in the "second" diagonal
1149 Solvers
-
842 Solvers
-
640 Solvers
-
590 Solvers
More from this Author5
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!