The optimized version makes the "gnome" (represented by the 'current' iterator here) leap back to the right i.e. to the place where it needed to swap the flower pots on its farthest reached position so far. This also avoids redundant comparisons. On average the optimization results in cutting the time by one/third on a modern x86 machine. (A wise gnome sort? :). However, the optimization works best if the iterators are random access iterators. Otherwise the algorithm works almost like the non-optimized version - it still avoids redundant comparisons though.

SUBROUTINE gnome_sort(LIST,LENGTH)INTEGER LIST,LENGTH,I,TEMPDIMENSION LIST(LENGTH)I=2WHILE(I.LE.LENGTH)DO IF((I.EQ.1).OR.(LIST(I-1).LE.LIST(I)))THEN I=I+1ELSE TEMP=LIST(I)LIST(I)=LIST(I-1)LIST(I-1)=TEMPI=I-1IF(I.EQ.1)THEN I=2END IF END IF END WHILE END