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

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


        
20条回答
  •  遥遥无期
    2020-12-09 07:33

    We can use a Bubble Sort for sorting.

    The best-case performance of O(n). Otherwise, best-case == worse-case == average-case == O(n^2)

    $Obj1 = array(20, 30, 10, 50, 40, 60, 100, 90, 80, 70);
    
     $temp;
    
    print_r($Obj1);
    
    
    for ($i = 0; $i < sizeof($Obj1) - 1; $i++) {
    
    for ($j = sizeof($Obj1) - 1; $j > $i; $j--) {
        if ($Obj1[$j - 1] > $Obj1[$j]) {
            $temp = $Obj1[$j-1];
            $Obj1[$j-1] = $Obj1[$j];
            $Obj1[$j] = $temp;
            $temp = 0;
        }
    }
    
    }
    
    print_r($Obj1);
    

提交回复
热议问题