1

how can I make this php bubble sort code better or more effective?

// 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 
Cœur
  • 32,421
  • 21
  • 173
  • 232
shayne
  • 81
  • 6
  • Check the response of this one http://stackoverflow.com/questions/9001294/bubble-sort-implementation-in-php , maybe this one can help you. – Sergi Case May 11 '16 at 14:46
  • 2
    maybe this is better for http://codereview.stackexchange.com/, these kind of questions. – izk May 11 '16 at 14:46
  • most effective is http://php.net/manual/en/function.sort.php :-) – Alex May 11 '16 at 15:52
  • I know it has rained a lot since this question, but if anyone wants to make the bubble sort efficient without using another algorithm as @Asur recommends (which is the best option), they should know that there are a couple of pretty well known tricks that do speed up the algortihm -> https://stackoverflow.com/questions/16195092/optimized-bubble-sort-java – guilleamodeo Mar 26 '18 at 17:14

3 Answers3

4

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:

// 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.

Hope this helps you!

Asur
  • 2,950
  • 1
  • 19
  • 31
  • I disagree with you, you have different sorting algorithms for different situations and its up to you as a developer to pick the right algorithm. You should want to optimize your algorithm to get the best results possible. – izk May 11 '16 at 14:49
  • 3
    @baboizk Yes of course, it always depends on the situation, that's why I said _generally_, if you optimize it (as is normally done) you would be basing your code on that algorithm but not making bubble algorithm better, as I see it, but you have a point. – Asur May 11 '16 at 14:54
1

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. Well it's a small speedup since count is O(1), but it's something..
Sidenote: there would be side effects if you modify the array while looping over it in php7 example:

$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
1

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

note how it only goes to 1

user3528269
  • 368
  • 3
  • 15
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 
GertG
  • 869
  • 1
  • 8
  • 21