Cody

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

38.71% Correct | 61.29% Incorrect
Last solution submitted on Mar 10, 2015

Problem Comments

Solution Comments

Tags

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!

Join the 15-year community celebration.

Play games and win prizes!

Learn more