➤ BubbleSort Algorythm.
function bubbleSort(&$array)
{
	if ($length = count($array)) {
		for ($i = 0; $i < $length; $i++) {
			for ($k = 0; $k < $length; $k++) {
				if ($array[$i] < $array[$k]) { // > descending
					$tmp = $array[$i];
					$array[$i] = $array[$k];
					$array[$k] = $tmp;
				}
			}
		}
	}
}
$arr = [2,99,15,72,14,1,26,923,34,66,7];
bubbleSort($arr);
print_r ($arr); // outputs: 1 2 7 14 15 26 34 66 72 99 923
➤ InsertSort Algorythm.
function insertSort(&$array) {
	if ($length = count($array)) {
		for ($i = 0; $i < ($length - 1); $i++) {
			$key = $i + 1;
			$tmp = $array[$key];
			for ($k = ($i + 1); $k > 0; $k--) {
				if ($tmp < $array[$k - 1]) { // > descending
					$array[$k] = $array[$k - 1];
					$key = $k - 1;
				}
			}
		        $array[$key] = $tmp;
		}
	}
}
$arr = [2,99,15,72,14,1,26,923,34,66,7];
insertSort($arr);
print_r ($arr); // 1 2 7 14 15 26 34 66 72 99 923
by Levani Makhareishvili
2 years ago
PHP
Arrays
Algorithms
1
Pro tip: use ```triple backticks around text``` to write in code fences