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 static void GnomeSort(ref int[] data)
{
	for (int i = 1; i < data.Length;)
	{
		if (data[i - 1] <= data[i])
			++i;
		else
		{
			int tmp = data[i];
			data[i] = data[i - 1];
			data[i - 1] = tmp;
			--i;
			if (i == 0)
				i = 1;
		}
	}
}
								


Example

									int[] data = { -1, 25, -58964, 8547, -119, 0, 78596 };
GnomeSort(ref data);
								


Output

									-58964
-119
-1
0
25
8547
78596