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

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


        
相关标签:
20条回答
  • 2020-12-09 07:40
        $arr= array(110,20,130,100,2);
    
        for($i=0; $i<count($arr)-1; $i++)
        {
            for($j=0; $j<count($arr)-1; $j++)
            {
                if($arr[$j]> $arr[$j+1]){
                    $temp= $arr[$j+1];
                    $arr[$j+1]= $arr[$j];
                    $arr[$j]= $temp;
                }
            }
    
        }
        print_r($arr);
    
    0 讨论(0)
  • 2020-12-09 07:42

    Here is the simplest way of sorting...

    function sort($arr) {
      for ($i=0; $i<count($arr); $i++) {
        for ($j=0; $j<count($arr)-1-$i; $j++) {
            if ($arr[$j+1] < $arr[$j]) {
                swap($arr, $j, $j+1);
            }
         }
      }
      return $arr;
    }
    
    function swap(&$arr, $a, $b) {
       $tmp = $arr[$a];
       $arr[$a] = $arr[$b];
       $arr[$b] = $tmp;
    }
    
    //using sorting functions
    $arr = array(1,13,2,9,5,7,0,3);
    
     echo("Before sorting");
     print_r($arr);
    
     sort($arr);
    
     echo("Sorted array");
     print_r($arr);
    
    0 讨论(0)
  • 2020-12-09 07:42
    $array = array('1','2','4','3','8','7','10','11');
    
    $c = count($array);
    for($i=0;$i<$c;$i++)
    {
        if($array[$i] > $array[$i+1]) {
                $temp = $array[$i+1];
                $array[$i+1]=$array[$i];
                $array[$i]=$temp;
            }  
    }
    
    echo "Sorted Array is: ";
    echo "<br /><pre>";
    print_r($array);
    
    0 讨论(0)
  • 2020-12-09 07:43

    Check this one , basic logic for beginners step by step

    <?php 
    
             $array = array('5','15','7','12','39','1','5');
    
             echo "Before Sort ";
             echo "<pre>";
             print_r($array);
    
             echo "<BR/>After Sort ";
             for($prnt_index=0; $prnt_index < count($array); $prnt_index++){
    
    
                // echo $array[$prnt_index].", ";
    
                for($child_index=0; $child_index < count($array)-1 ; $child_index++){
    
                    if($array[$child_index] > $array[$child_index+1] ){     // 9, 7
    
                        //apply swapping concept
                        $temp_var= $array[$child_index+1];  //put 7 in some temp var 
    
                        //  swap current index with next index
                        $array[$child_index+1] =$array[$child_index];
    
                        // get temp data and put on current index 
    
                        $array[$child_index] =$temp_var; 
                    }
    
                }   
             }
    
    
             print_r($array);
             echo "</pre>";
    
            ?>
    

    Output

    0 讨论(0)
  • 2020-12-09 07:45

    Here is the best way of sorting. Also check response time.

     function findMaxVal($arr)
     {
      $maxNum = $arr[0];
    
      foreach($arr as $arval)
      {
       $maxNum = $maxNum < $arval ? $arval : $maxNum;
      }
    
      return $maxNum;
     }
    
     function sortArrFun($arr)
     {
      $maxVal = findMaxVal($arr);
      $countArr = array();
    
      for($i = 0; $i <= $maxVal; $i++)
      {
       $countArr[$i] = 0;
      } 
    
      for($i = 0; $i < count($arr); $i++)
      {
       $countArr[$arr[$i]]++;
      }
    
      $order = array();
      for($i = 0; $i < count($countArr); $i++)
      {
       for($j = 0; $j < $countArr[$i]; $j++)
       {
        $order[] = $i;
       }
      }
    
      return $order;
     }
    
     $arrr = [1,22,333,45,5,8888,9999,0,9,7,8]
     print_r(sortArrFun($arrr1));
    
    0 讨论(0)
  • 2020-12-09 07:46

    You can refer this program.

    <?php
    
    //using sorting functions
    $arr = array(1,13,2,9,5,7,0,3);
    
    echo("Before sorting");
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    
    for ($i=0; $i < count($arr)-1; $i++) {
    	sortarray($arr);
    }
    
    function sortarray(&$arr) {
        for ($i=0; $i < count($arr)-1; $i++) {
            if ($arr[$i+1] < $arr[$i]) {
                $tmp = $arr[$i+1];
                $arr[$i+1] = $arr[$i];
                $arr[$i] = $tmp;
            }
        }
    }
    
    echo("Sorted array");
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题