Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,38 @@
|
|||
function list = mergeSort(list)
|
||||
|
||||
if numel(list) <= 1
|
||||
return
|
||||
else
|
||||
middle = ceil(numel(list) / 2);
|
||||
left = list(1:middle);
|
||||
right = list(middle+1:end);
|
||||
|
||||
left = mergeSort(left);
|
||||
right = mergeSort(right);
|
||||
|
||||
if left(end) <= right(1)
|
||||
list = [left right];
|
||||
return
|
||||
end
|
||||
|
||||
%merge(left,right)
|
||||
counter = 1;
|
||||
while (numel(left) > 0) && (numel(right) > 0)
|
||||
if(left(1) <= right(1))
|
||||
list(counter) = left(1);
|
||||
left(1) = [];
|
||||
else
|
||||
list(counter) = right(1);
|
||||
right(1) = [];
|
||||
end
|
||||
counter = counter + 1;
|
||||
end
|
||||
|
||||
if numel(left) > 0
|
||||
list(counter:end) = left;
|
||||
elseif numel(right) > 0
|
||||
list(counter:end) = right;
|
||||
end
|
||||
%end merge
|
||||
end %if
|
||||
end %mergeSort
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>> mergeSort([4 3 1 5 6 2])
|
||||
|
||||
ans =
|
||||
|
||||
1 2 3 4 5 6
|
||||
Loading…
Add table
Add a link
Reference in a new issue