php sorting algorithem

hello everyone i am trying to create an algorithm where foreach of these number in array i need to do bubble sort except for the first one so i was was able to do for the second index of the array but unable to detect how to go on for all. can can anyone guide me here i will give a picture where it shows an example output. thank u in advance

$x = array (9,7,5,3,0);

    $count = count($x);
    for($i = 0; $i < $count-1; $i++ ) {
        $temp = $x[$i+1];
        $x[$i+1] = $x[$i];
        $x[$i] = $temp;
        echo '<pre>';
        print_r($x);
    }

currently i am able to get this output

Array
    (
        [0] => 7
        [1] => 9
        [2] => 5
        [3] => 3
        [4] => 0
    )
    Array
    (
        [0] => 7
        [1] => 5
        [2] => 9
        [3] => 3
        [4] => 0
    )
    Array
    (
        [0] => 7
        [1] => 5
        [2] => 3
        [3] => 9
        [4] => 0
    )
    Array
    (
        [0] => 7
        [1] => 5
        [2] => 3
        [3] => 0
        [4] => 9
    )

i need for other number also such as after 7 it should be with 5
    57390
    53790
    53970
    53907
    and then for 3
    35079
    30579
    30759
    30795
    and then for 0
    should be same for all 4 numbers like
    03570
    03579
    03579
    03579


![sort|338x486](upload://1UeNfUjeaKK3XBtWNtYun0fcqZu.png)

i don’t get what you want to do, your code just switches numbers, that’s not sorting.

Sponsor our Newsletter | Privacy Policy | Terms of Service