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

New to MATLAB?

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.

Tags

Problem Group

Solution Statistics

11 correct solutions 19 incorrect solutions
Last solution submitted on Feb 17, 2015

Problem Comments

Solution Comments