Why am I getting a "Not enough input arguments." error in my function?
2 views (last 30 days)
Show older comments
Not really familiar with MATLAB but I have a couple of sorting functions and would like to compare their running times with the function FindRunningTime:
function t = FindRunningTime(SortAlg)
n = 10000.*randn(1,20);
if strcmp(SortAlg, 'InsertionSort')
tic;
InsertionSort(n);
t = toc;
else
tic;
MergeSort(n);
t = toc;
end %if
end %FindRunningTime
where SortAlg is specified as merge or insert. However when running the code I get the error "Not enough input arguments." on line 2 of both sorting algs. Hopefully someone could point me in the right direction. Also if anyone knows of a nice way to specify the random number generators range, that would be helpful. Below is the code for both sorting algorithms:
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 = sort(left);
right = sort(right);
if left(end) <= right(1)
list = [left right];
return
end
%merge(left,right)
counter = 1;
%index placeholder for final list
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
function list = InsertionSort(list)
for i = (2:numel(list))
value = list(i);
j = i - 1;
while (j >= 1) && (list(j) > value)
list(j+1) = list(j);
j = j-1;
end %while
list(j+1) = value;
end %for
end %InsertionSort
2 Comments
Answers (1)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!