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.



									function GnomeSort(&$data)
{
	$dataCount = count($data);
	for ($i = 1; $i < $dataCount;)
	{
		if ($data[$i - 1] <= $data[$i])
			++$i;
		else
		{
			$tmp = $data[$i];
			$data[$i] = $data[$i - 1];
			$data[$i - 1] = $tmp;
			--$i;
			if ($i == 0)
				$i = 1;
		}
	}
}
								


Example

									$data = array(-1, 25, -58964, 8547, -119, 0, 78596);
GnomeSort($data);
								


Output

									-58964
-119
-1
0
25
8547
78596