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

2 views (last 30 days)
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
Jan
Jan on 21 Feb 2013
Edited: Jan on 21 Feb 2013
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 on 21 Feb 2013
Edited: Nicholas Colburn on 21 Feb 2013
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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 20 Feb 2013
Hint:
if strcmp(SortAlg, 'InsertionSort')
  4 Comments

Sign in to comment.

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!