Gnome Sort

Gnome sort also known as stupid sort is a sorting algorithm which is similar to insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. It is conceptually simple, requiring no nested loops.



									Public Shared Sub GnomeSort(ByRef data As Integer())
	Dim i As Integer = 1
	While i < data.Length
		If data(i - 1) <= data(i) Then
			i += 1
		Else
			Dim tmp As Integer = data(i)
			data(i) = data(i - 1)
			data(i - 1) = tmp
			i -= 1
			If i = 0 Then
				i = 1
			End If
		End If
	End While
End Sub
								


Example

									Dim data As Integer() = {-1, 25, -58964, 8547, -119, 0, 78596}
GnomeSort(data)
								


Output

									-58964
-119
-1
0
25
8547
78596