Why am I getting a "Not enough input arguments." error in my function?

2 次查看(过去 30 天)
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 个评论
Jan
Jan 2013-2-21
编辑:Jan 2013-2-21
The line 2 of MergeSort is empty, so I do not believe that it causes an error. Please post the complete error message also instead of describing it.
What do you mean by "specify the random number generators range"?
Nicholas Colburn
Nicholas Colburn 2013-2-21
编辑:Nicholas Colburn 2013-2-21
Apologies, that would be line 3 of both sorts. Here is the error, it's the same for both; there isn't much to it:
"Error using InsertionSort (line 3) Not enough input arguments."
As far as your second question, I would like to generate a sequence of numbers between 0 and 10,000 using (I'm assuming) some form of the rand() function.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2013-2-20
Hint:
if strcmp(SortAlg, 'InsertionSort')
  4 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by