-2

Is my algorithm faster then php sort() ?

algo:

        $a = array(0,3,4,2,7,6,6,8,6,1);
        $z = count($a)-1;
        for($i = 0;$i < $z; $i++){
           if($a[$i] > $a[$i+1]){
              $temp = $a[$i];
              $a[$i] = $a[$i+1];
              $a[$i+1] = $temp;
              $i = -1;
           }   
        }
        echo "<pre>";
        print_r($a);
        echo "</pre>";

compare with php sort() ... let me know your results as i have bad internet...

alin100
  • 11
  • 4
  • No it's not. This is not a place where people will compare your code to native PHP methods and give you results. – vuryss Aug 04 '20 at 21:02
  • 1
    Why is `$i` an array? You never use anything other than `$i[0]`. – Barmar Aug 04 '20 at 21:03
  • Of course, interpreted PHP code is always faster than optimized and compiled C binary. – AbraCadaver Aug 04 '20 at 21:03
  • 1
    Your algorithm looks like [bubble sort](https://en.wikipedia.org/wiki/Bubble_sort). – Barmar Aug 04 '20 at 21:04
  • well i used microtime and my algo was faster on my pc ... this is why i am asking ... to see if i did not get fake results. – alin100 Aug 04 '20 at 21:04
  • 1
    @alin100 Try sorting an array with a million elements and see how they compare. – Barmar Aug 04 '20 at 21:05
  • You know that you don't need internet access to run a sorting algorithm, right? And also no, a userland implementation of bubble sort is not going to be faster than the language's own method. – iainn Aug 04 '20 at 21:06
  • $i[0] is to reset for loop when it finds 4 ,2 – alin100 Aug 04 '20 at 21:06
  • @iainn did you compare it to say that sort() is faster then that algo ? – alin100 Aug 04 '20 at 21:08
  • Compare and prove that is slower then sort(); this is what i am asking . Thank you ! – alin100 Aug 04 '20 at 21:09
  • @alin100 But why is it `$i[0]` and `$z[0]` instead of just `$i` and `$z`? These variables don't need to be arrays, just ordinary variables. – Barmar Aug 04 '20 at 21:14
  • it didn't reset $i do to unknown reason and i changed it to $i[0] ,worked with $i[0],if i do $i instead of $i[0] i get like a infinite loop ... white screen – alin100 Aug 04 '20 at 21:17
  • @Barmar if i change $z[0] to $z i get white screen and page loading forever ... – alin100 Aug 04 '20 at 21:23
  • You also have to change to `$z = count($a) - 1;` so it's not put into an unnecessary array. – Barmar Aug 04 '20 at 21:26
  • @Barmar thank you for noticing that ... it solved that $z and $i thing – alin100 Aug 04 '20 at 21:30

1 Answers1

1

As pointed out in What sort algorithm does PHP use?, the built-in sorting algorithm is Quicksort. The average performance of Quicksort is O(n log n).

Your algorithm is similar to Bubble Sort. Its average performance is O(n2). But since your code goes back to the beginning after each swap, which is unnecessary, it's even worse than bubble sort.

For large arrays, Quicksort will be significantly faster than Bubble Sort.

Also, the sort() function is implemented in C code, which is compiled to machine code. Using a sorting algorithm written in PHP will have additional overhead of the PHP interpreter.

You also have a number of unnecesary arrays $i and $z, which should just be ordinary variables.

$a = array(0,3,4,2,7,6,6,8,6,1);
$z = count($a)-1;
for($i = 0;$i < $z; $i++){
    if($a[$i] > $a[$i+1]){
        $temp = $a[$i];
        $a[$i] = $a[$i+1];
        $a[$i+1] = $temp;
        $i = -1;
    }   
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thank you for seeing that i changed it ... – alin100 Aug 04 '20 at 21:33
  • in bubble sort https://en.wikipedia.org/wiki/Bubble_sort i noticed that it does not reset once you make switch of 2 value,it goes to the end of array ... in my code when you switch / flip values , it goes back to 0 and starts over ... it's a variation of bubble sort ... – alin100 Aug 04 '20 at 21:38
  • That probably makes your algorithm worse, since there' no need to go back to the beginning. – Barmar Aug 04 '20 at 21:39
  • who knows we may find a use for it one day :) – alin100 Aug 04 '20 at 21:42