Discover MakerZone

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

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!

Problem 1056. Partial sorting algorithm

Created by Paul Peeling

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.


Problem Group

Solution Statistics

12 correct solutions 19 incorrect solutions
Last solution submitted on Mar 10, 2015

Problem Comments

Solution Comments