Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
function list = pancakeSort(list)
|
||||
|
||||
for i = (numel(list):-1:2)
|
||||
|
||||
minElem = list(i);
|
||||
minIndex = i;
|
||||
|
||||
%Find the min element in the current subset of the list
|
||||
for j = (i:-1:1)
|
||||
if list(j) <= minElem
|
||||
minElem = list(j);
|
||||
minIndex = j;
|
||||
end
|
||||
end
|
||||
|
||||
%If the element is already in the correct position don't flip
|
||||
if i ~= minIndex
|
||||
|
||||
%First flip flips the min element in the stack to the top
|
||||
list(minIndex:-1:1) = list(1:minIndex);
|
||||
|
||||
%Second flip flips the min element into the correct position in
|
||||
%the stack
|
||||
list(i:-1:1) = list(1:i);
|
||||
|
||||
end
|
||||
end %for
|
||||
end %pancakeSort
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>> pancakeSort([4 3 1 5 6 2])
|
||||
|
||||
ans =
|
||||
|
||||
6 5 4 3 2 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue