简体   繁体   English

冒泡排序数组,我怎样才能使这个php冒泡排序代码更好或更有效?

[英]Bubble sort array, how can I make this php bubble sort code better or more effective?

how can I make this php bubble sort code better or more effective? 如何让这个php冒泡排序代码更好或更有效?

// bubble sort for $AR 
for($i = count($AR)-1 ; $i <= 1; $i--) 
   for($j = 0; $j < $i; $j++) {
      if($AR[$j] > $AR[$j+1]) {
          $t = $AR[$j];
          $AR[$j] = $AR[$j+1];
          $AR[$j+1] = $t;
       } //if
   } //for j 

Algorithms are algorithms, if you make some modifications and achieve a better performance you won't be using the Bubble sorting algorithm anymore, because it would have changed. 算法是算法,如果你进行一些修改并获得更好的性能,你将不再使用冒泡排序算法,因为它会发生变化。

If you want a increase performance you need to change the algorithm, Quick Sort is generally considered to be the best sorting one. 如果您想要提高性能,则需要更改算法, 快速排序通常被认为是最佳排序算法。 As an example of the implementation in php: 作为php中的实现示例:

// QuickSort Algorithm function
function quickSort(array $array) {
    if (count($array) == 0) {
        return $array;
    }
    $pivot = $array[0];
    $left = $right = array();
    for($i = 1; $i < count($array); $i ++) {
        if ($array[$i] < $pivot) {
            $left[] = $array[$i];
        } else {
            $right[] = $array[$i];
        }
    }
    return array_merge(quickSort($left), array(
            $pivot
    ), quickSort($right));
} 

Of course, it always depends on the situation, if you optimize it you would be basing your code on that algorithm but not making bubble algorithm better or more effective . 当然,它总是取决于情况,如果你优化它,你将基于该算法的代码,但不是使泡沫算法更好或更有效

Check this post , where is really well documented almost every type of sorting in php. 查看这篇文章 ,几乎所有类型的PHP排序都记录在哪里。

Hope this helps you! 希望这对你有所帮助!

If you want to make this one more efficient, you could put the 如果你想让这个更高效,你可以把它

count($AR)-1

in your for loop into a variable, since it would be done at every iteration. 在你的for循环中变量,因为它将在每次迭代时完成。 Well it's a small speedup since count is O(1), but it's something.. 好吧,这是一个小的加速,因为计数是O(1),但它是...
Sidenote: there would be side effects if you modify the array while looping over it in php7 example: 旁注:如果在php7示例中循环遍历数组时修改数组会有副作用:

$a = [1,2,3,4];  
$b = 1;   
for ($i = 0;$i<count($a);$i++) {  
unset($a[$i]);  
echo $i . "\n";  
}  
var_dump($a);  

output: 输出:

0 0
1 1

-:9: - :9:
array(2) { 数组(2){
[2] => [2] =>
int(3) INT(3)
[3] => [3] =>
int(4) INT(4)
} }

note how it only goes to 1 请注意它只是1

As it stands, your code won't do anything at all. 就目前而言,您的代码根本不会做任何事情。 You got the first loop condition wrong. 你的第一个循环条件错了。 With that corrected, it looks like a perfectly fine bubble sort. 经过纠正后,它看起来像一个完美的泡泡排序。

// bubble sort for $AR 
for($i = count($AR)-1 ; $i >= 1; $i--) 
   for($j = 0; $j < $i; $j++) {
      if($AR[$j] > $AR[$j+1]) {
          $t = $AR[$j];
          $AR[$j] = $AR[$j+1];
          $AR[$j+1] = $t;
       } //if
   } //for j 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM