Selection Sort

Selection sort is a sorting algorithm. It works by selecting the smallest element of the array and placing it at the head of the array. Then the process is repeated for the remainder of the array, the next largest element is selected and put into the next slot, and so on down the line.



									function SelectionSort(&$data, $count) {
	for ($i = 0; $i < $count - 1; $i++)
	{
		$min = $i;

		for ($j = $i + 1; $j < $count; $j++)
		{
			if ($data[$j] < $data[$min])
			{
				$min = $j;
			}
		}

		$temp = $data[$min];
		$data[$min] = $data[$i];
		$data[$i] = $temp;
	}
}
								


Example

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


Output

									-58964
-119
-1
0
25
8547
78596