Gnome sort?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (2)
Walter Roberson
am 20 Feb. 2012
Presumably "list" should be initialized to the list of values to be sorted.
2 Kommentare
Jan
am 20 Feb. 2012
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 Kommentare
Jan
am 20 Feb. 2012
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
am 20 Feb. 2012
Perhaps there was a reason that the original code did not initialize i to 1 ?
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!