bubble sort php

Solutions on MaxInterview for bubble sort php by the best coders in the world

showing results for - "bubble sort php"
Edna
08 Sep 2019
1<?php
2function bubble_Sort($my_array )
3{
4	do
5	{
6		$swapped = false;
7		for( $i = 0, $c = count( $my_array ) - 1; $i < $c; $i++ )
8		{
9			if( $my_array[$i] > $my_array[$i + 1] )
10			{
11				list( $my_array[$i + 1], $my_array[$i] ) =
12						array( $my_array[$i], $my_array[$i + 1] );
13				$swapped = true;
14			}
15		}
16	}
17	while( $swapped );
18return $my_array;
19}
20 $test_array = array(3, 0, 2, 5, -1, 4, 1);
21echo "Original Array :\n";
22echo implode(', ',$test_array );
23echo "\nSorted Array\n:";
24echo implode(', ',bubble_Sort($test_array)). PHP_EOL;
25?>
26
27