sorting array value without using built in php like sort() etc

后端 未结 20 1367
栀梦
栀梦 2020-12-09 07:10


        
相关标签:
20条回答
  • 2020-12-09 07:46
    //Best solution for bubble sort
    $a = [10,5,2,8,7];
    $k = 0;//I used this variable because i want to show you how many times my for loop needs to iterate maximum.
    for($i = 0;$i < count($a); $i++){
        for($j = 1; $j < count($a) - $i; $j++){
            if($a[$j -1 ] > $a[$j]){
                $temp = $a[$j];
                $a[$j] = $a[$j -1];
                $a[$j - 1] = $temp;
            }
            $k++;
        }
    
    }
    echo $k;
    echo '<pre>';
    print_r($a);
    echo '</pre>';
    
    0 讨论(0)
  • 2020-12-09 07:50
    <?php
    $array  = array(1,4,10,5,8,3,6,61,0);
    
    for($x=0;$x<=count($array)-1;$x++){
    
        for($z=0;$z<=count($array)-1;$z++){
    
        if($array[$x]<$array[$z])
        {
            $temp = $array[$x];
            $array[$x] = $array[$z];
            $array[$z] = $temp;
        }
    
        }   
    }
    
    print_r($array);
    
    0 讨论(0)
  • 2020-12-09 07:50

    This is my Quicksort algorithm in PHP:

    <?php
    $array = [1, 4, 3, 5, 9, 6, 1, 6, 4, 1, 1, 4, 5, 6, 6, 7, 2, 1, 4, 0];
    $j = count($array);
    $t = $j-1;
    while($j>=0){
        for ($i=0; $i < $t; $i++) { 
            $aux = $array[$i]; 
            if($array[$i]>$array[$i+1]){
                $array[$i] = $array[$i+1];
                $array[$i+1] = $aux;
            }
        }
        $j--;
    }
    print_r($array);
    
    0 讨论(0)
  • 2020-12-09 07:51

    Here is the right solution in php:

    $array=array('2','4','8','5','1','7');
    
    for($i=1;$i< count($array);$i++)
    {
       for($j=$i;$j>0;$j--)
       {    
           if($array[$j] < $array[$j-1])
           { 
               $tmp = $array[$j];
               $array[$j] = $array[$j-1];
               $array[$j-1] = $tmp ;
           }
       }
    }
    
    0 讨论(0)
  • 2020-12-09 07:53
    //Here is the simplest way of sorting...
    $list = array('5','15','7','12','39','1','5');
    $a;
    $b;
    for($i=0;$i<count($list);$i++){ 
        for($j=0;$j<count($list);$j++){
            if($list[$i] < $list[$j]){
                $a = $list[$j]; 
                $b = $list[$i];     
    
                $list[$i] = $a;
                $list[$j] = $b;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:54

    All the accepted answers here are good, and most of them use two for loops to sort an array. At first the code seemed fairly straight and even I thought of the same. But then I wanted to investigate further. How efficient is this method? So I created an array of a 10,000 "count" or values and wrote it in a file to be included later on, for consistency, using the following for code:

    $str = "<?php \n \$array = array( \n";
    for($x = 0; $x <= 10000; $x++){
        $str .= mt_rand(0,10000).",\n";
    }
    $str .= "); \n ?>";
    
    $file = fopen('req_arr.php', 'w+');
    echo fwrite($file,$str);
    fclose($file);
    
    include_once('req_arr.php');
    
    $arr = $array;
    

    Then I used the two for loops method as given by most of the guys here, and also measured the time taken:

        $start = microtime(1);
        $cnt = count($arr);
        for($i = 0; $i < $cnt; $i++ ){
            for($j = 0; $j < $cnt-1; $j++ ){
                $temp = '';
                if($arra[$j] > $arra[$j+1]){
                    $temp = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1] = $temp;
                }
            }
        }
        $stop = microtime(1);
        echo $stop - $start;
        echo '<pre>'; print_r($arr);
    

    And this gave the execution time (in seconds) to be 7.5408220291138.

    Note: This code was tested in XAMPP on Windows10, 64 bit, i7 gen 4, 8 GB RAM, and in Chrome.

    This is way too much. I'm sure PHP can't be this sloppy. So next I tested the in-built PHP rsort() function, using the following code:

    $start = microtime(1);
    rsort($arr, SORT_NUMERIC);
    $stop = microtime(1);
    echo $stop - $start;
    echo '<pre>'; print_r($arr);    
    

    This time, the execution time was just 0.0033688545227051 seconds. JUST 0.0033688545227051 SECONDS for sorting a 10,000 values array. Clearly, the two for loop method is inefficient to whatever PHP is using in its core.

    A quick research on Google/PHP.net gave me the answer that PHP uses quicksort algorithm to sort indexed array, and that it doesn't uses two for loops but recursive function. I dug deeper and found a few examples of quicksearch for C++, Java etc. So, I replicated them in PHP, as follows:

    /*
        The main function that implements QuickSort
        arr --> Array to be sorted,
        low  --> Starting index,
        high  --> Ending index
    */
    function quickSort(&$arr, $low, $high)
    {
        if ($low < $high)
        {
            /* pi is partitioning index, arr[p] is now
               at right place */
            $pi = partition($arr, $low, $high);
            // Separately sort elements before
            // partition and after partition
            quickSort($arr, $low, $pi - 1);
            quickSort($arr, $pi + 1, $high);
        }
    
        return $arr;
    }
    
    function partition (&$arr, $low = 0, $high)
    {
        $pivot = $arr[$high];  // pivot
        $i = ($low - 1);  // Index of smaller element
    
        for ($j = $low; $j <= $high-1; $j++)
        {
            // If current element is smaller than or
            // equal to pivot
            if ($arr[$j] <= $pivot)
            {
                $i++;    // increment index of smaller element
                swap($arr[$i], $arr[$j]);
            }
        }
        swap($arr[$i + 1], $arr[$high]);
        return ($i + 1);
    }
    
    function swap(&$a, &$b){
        $t = $a;
        $a = $b;
        $b = $t;
    }
    

    Obviously, this could be further optimized but I just wanted to get something running and see the results, and this was sufficient. So, now let's see the results:

    $start = microtime(1);
    $sarr = quickSort($array, 0, $cnt-1);
    $stop = microtime(1);
    echo $stop - $start;
    echo '<pre>';print_r($sarr);
    die();
    

    The time taken by this algorithm came out be: 0.022707939147949

    Still, not as fast as rsort() but satisfactory. I tried the same with a million values array too but the two for loops array just exhausted the memory and I decided even 10,000 value array proves the theory well.

    Cheerrrssss...

    0 讨论(0)
提交回复
热议问题