Gnome sort?

I have a question about gnome sorts. I cannot seem to find a code anywhere on the internet that would give me some clue about how to going about creating a gnome sort. I have found this:
function list = gnomeSort(list)
i = 2;
j = 3;
while i <= numel(list)
if list(i-1) <= list(i)
i = j;
j = j+1;
else
list([i-1 i]) = list([i i-1]);
i = i-1;
if i == 1
i = j;
j = j+1;
end
end
end
but it tells me "Input argument "list" is undefined" so how would I go about fixing this? Any help would be greatly appreciated!

답변 (2개)

Walter Roberson
Walter Roberson 2012년 2월 20일

0 개 추천

Presumably "list" should be initialized to the list of values to be sorted.

댓글 수: 2

Becky
Becky 2012년 2월 20일
I am really new to Matlab, so everything people say is like in a foreign language to me haha! What do you mean by it should be initialized to the list of values to be sorted? What would I do to the code to do this? Sorry for being so rubbish!
Walter Roberson
Walter Roberson 2012년 2월 20일
list = [17 39 2 6.8 pi];

댓글을 달려면 로그인하십시오.

Jan
Jan 2012년 2월 20일

0 개 추천

To learn the basics of Matlab read the Getting Started chapters in the documentation.
For this problem you have to define the input of the function, e.g.:
list = randperm(20);
list = gnomeSort(list)

댓글 수: 3

Becky
Becky 2012년 2월 20일
I've made a few alterations:
N=input('N=');
for i = 1
for j = 1:N
a(i,j)=input(strcat('a(',int2str(i),',',int2str(j),')='));
while i <= numel(a)
if a(i-1) <= a(i)
i = j;
j = j+1;
else
a([i-1 i]) = a([i i-1]);
i = i-1;
if i == 1
i = j;
j = j+1;
end
end
end
disp(a)
end
end
But it's getting stuck at "if a(i-1) <= a(i)" because if i=1, it comes up with zero, which obviously isn't a positive integer, so comes up with an error. Any idea how to fix it?
Jan
Jan 2012년 2월 20일
The outer "for i=1" loop is not a loop, so avoid it.
I do not know what you want to achieve. Therefore it is hard to guess, how the code should be fixed.
Walter Roberson
Walter Roberson 2012년 2월 20일
Perhaps there was a reason that the original code did not initialize i to 1 ?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2012년 2월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by