[php classic algorithm] bubble sort, bubble sort principle, bubble sort execution logic, execution process, execution result code

The principle of bubble sort is
to compare two adjacent elements each time and swap the larger element to the right end.

Bubble sort execution process output effect

[php classic algorithm] bubble sort, bubble sort execution logic, execution process, execution result code

Bubble sort implementation ideas

Each bubble sorting operation will compare two adjacent elements to see if they meet the size relationship requirements. If not, the order of the two adjacent elements will be exchanged. At least one element will be moved to where it should be in a bubble sort operation. Repeat the arranged position N times to complete the bubble sort.

bubble sort code

 		$array=[2,31,4,6,1,8,21,34,23];
        for($i=0; $i<count($array)-1; $i++){
    
    
            for($ij=0; $ij<count($array)-1-$i; $ij++){
    
    
                if ($array[$ij] > $array[$ij+1]){
    
    
                    $temp = $array[$ij];
                    $array[$ij]=$array[$ij+1];
                    $array[$ij+1]=$temp;
                }
            }
        }

Guess you like

Origin blog.csdn.net/gjwgjw1111/article/details/133076525