Sorting an array with bubble sort [duplicate]

我的梦境 提交于 2021-02-04 08:23:39

问题


I am trying to create an algorithm that shows each step of bubble sort, sorting one number at a time. I was was able to sort the number at the first index, but I need to figure out how to sort all the numbers.

$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);
}

My current output is:

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
)

From here I need to continue sorting the remaining numbers. For 7, the output should be

57390
53790
53970
53907

and then for 3

35079
30579
30759
30795

and then for 0, it should be same for all 4 lines like

03570
03579
03579
03579

[The assigned problem.][1]


回答1:


Two issues:

  • You should only swap values when they are not in the right order
  • You need an outer loop to repeat this inner loop, similar to a proper bubble sort algorithm.

Your code can be modified like below so it generates the required output:

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

$count = count($x) - 1;
for($times = 0; $times < $count; $times++) {
    for($i = 0; $i < $count; $i++ ) {
        $temp = $x[$i+1];
        if ($temp < $x[$i]) {
            $x[$i+1] = $x[$i];
            $x[$i] = $temp;
        }
        echo implode(" ", $x) . "\n";
    }
    echo "\n";
}

Note that a proper bubble sort algorithm will perform fewer iterations.




回答2:


Bubble sort is basically simplified into a min() + shift (or sometimes swap) operation that occurs N times. So it's an O(n^2) algorithm.

Since this is for learning purposes I'm going to try to be as verbose as possible.

function getMin(Array &$array): Int {
    $min = reset($array);
    $minKey = null;

    // Find the minimum value
    foreach($array as $k => $n) {
        if ($n < $min) {
            $min = $n;
            $minKey = $k;
        }
    }

    // remove the minimum value from the array
    $array[$k] = null;
    $array = array_filter($array, function ($v) { return $v !== null; });

    return $min;
}

$array = [9,7,5,3,0];

foreach ($array as $n => $value) {
    // Find the min value in the array from Nth index onward
    $min = getMin($array); // get the smallest value in the array and remove it.
    $sorted[] = $min; // push it on to the new array
}

var_dump($sorted);

This gives you the expect result:

array(5) {
  [0]=>
  int(0)
  [1]=>
  int(3)
  [2]=>
  int(5)
  [3]=>
  int(7)
  [4]=>
  int(9)
}

Of course, this is not the way you would want to implement Bubble Sort, because it's a bit circuitous. Again, it's for educational purposes. You can inspect the $array as it's being modified at each step and the new $sorted as it's being built. Typically you would just swap the min/max values in place and use the same array (keeping track of the keys to rescan the array).

Like this...

// Unsorted $array
$array = [9,7,5,3,0];

// Sort the array
for ($lastKey = $i = 0, $len = count($array); $i < $len; $i++) {
    // Scan for minimum value
    for ($minKey = $j = $lastKey, $min = $array[$minKey]; $j < $len; $j++) {
        if ($array[$j] < $min) {
            $minKey = $j;
            $min = $array[$j];
        }
    }
    // Swap the values
    $swap = $array[$lastKey];
    $array[$lastKey] = $min;
    $array[$minKey] = $swap;

    // Update the scan position
    $lastKey++;
}

var_dump($array); // Gives you [0,3,5,7,9]



回答3:


You are performing unconditional position swapping, but you actually need to check if movement is required.

There is no shortage of tutorials that demonstrate a bubble sort in php. A quick good lead me to this one: https://www.w3resource.com/php-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-6.php which can be modified for your purposes.

PHP now offers "array destructuring" which means you no longer need to use a temporary holding variable while swapping.

Code: (Demo)

$x = [9,7,5,3,0];
$count = count($x) - 1;
for ($pass = 0; $pass < $count; ++$pass) {
    for ($i = 0; $i < $count; ++$i) {
        if ($x[$i] > $x[$i + 1]) {
            [$x[$i + 1], $x[$i]] = [$x[$i], $x[$i + 1]];
        }
        $results[] = implode($x);
    }
    $results[] = "\n";
}
echo implode("\n", $results);


来源:https://stackoverflow.com/questions/60010398/sorting-an-array-with-bubble-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!