Home > Gnome / Selection Sorts
Gnome sort - for nearly sorted lists. 1: I = 2 : J = 3 2: IF NM(I - 1) <= NM(I) GOTO 5 3: TP = NM(I) : NM(I) = NM(I - 1) : NM(I - 1) = TP 4: I = I - 1 : GOTO 6 5: I = J : J = J + 1 6: IF I = 1 THEN LET I = 2 7: IF I <= SI GOTO 2 8: RETURN SI - size of list NM() - number array I,J - array pointers TP - temporary holding for swapping Change line 2 to >= for decending order. ----------------------------------------------------------- Selection sort - for random lists. 1: I = 1 2: M = I : SW = 0 : J = I + 1 3: IF NM(J) < NM(M) THEN LET M = J : SW = 1 4: IF J < SI THEN LET J = J + 1 : GOTO 3 5: IF SW = 0 GOTO 7 6: TP = NM(I) : NM(I) = NM(M) : NM(M) = TP 7: IF I < (SI - 1) THEN LET I = I + 1 : GOTO 2 8: RETURN M - pointer to the minimum value found SW - swap flag Change line 3 to > for decending order. The array must contain at least 2 elements before sorting.