Cody

# Problem 1056. Partial sorting algorithm

Solution 2297399

Submitted on 19 May 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
feval(@assignin,'caller','score',250);

2   Pass
global totalNCompare totalNCompare = 0;

3   Fail
compareFcn = @(x,y) x(1) > y(1); y = [6 3 1 9 5]; x_correct = [9 6 5]; profile on x = partial_sort(y,3,compareFcn); assert(isequal(x,x_correct)) profile off stats = profile('info'); found = false; for i = 1:length(stats.FunctionTable) functionName = stats.FunctionTable(i).FunctionName; numCalls = stats.FunctionTable(i).NumCalls; if ~isempty(strfind(functionName,'@(x,y)x(1)>y(1)')) found = true; break end end assert(found) global totalNCompare totalNCompare = totalNCompare + numCalls;

x = 9 6 5

Assertion failed.

4   Fail
compareFcn = @(x,y) length(x) < length(y); y = {'a','bc','def','ghij','klmno','pqrstu'}; x_correct = {'a','bc','def','ghij'}; profile on x = partial_sort(y,4,compareFcn); assert(isequal(x,x_correct)) profile off stats = profile('info'); found = false; for i = 1:length(stats.FunctionTable) functionName = stats.FunctionTable(i).FunctionName; numCalls = stats.FunctionTable(i).NumCalls; if ~isempty(strfind(functionName,'@(x,y)length(x)<length(y)')) found = true; break end end assert(found) global totalNCompare totalNCompare = totalNCompare + numCalls;

Error using sort Only one input argument is supported for cell arrays. Error in partial_sort (line 2) ySorted = sort(y,'descend'); Error in Test4 (line 5) x = partial_sort(y,4,compareFcn);

5   Fail
compareFcn = @(x,y) mod(x(1),y(1)) == 0; y = [1 2 3 4 5 6 10 12 15 20 30 60]; x_correct = [60 30 12 20]; profile on x = partial_sort(y,4,compareFcn); assert(isequal(sort(x),sort(x_correct))) profile off stats = profile('info'); found = false; for i = 1:length(stats.FunctionTable) functionName = stats.FunctionTable(i).FunctionName; numCalls = stats.FunctionTable(i).NumCalls; if ~isempty(strfind(functionName,'@(x,y)mod(x(1),y(1))==0')) found = true; break end end assert(found) global totalNCompare totalNCompare = totalNCompare + numCalls;

x = 60 30 20 15

Assertion failed.

6   Pass
global totalNCompare feval(@assignin,'caller','score',totalNCompare);