Home >Backend Development >PHP Tutorial >PHP algorithm selection sort

PHP algorithm selection sort

不言
不言Original
2018-04-08 13:18:252472browse

The content introduced in this article is the selection sorting code in the PHP algorithm. Now I share it with you. Friends in need can also refer to it. Let’s take a look together.

<?php 
// //选择排序

  function SelectionSort($arr)
  {
    for($i = 0;$i<count($arr);$i++){
      
      $min = $i;

      for($j = $i;$j<count($arr);$j++){

        if($arr[$min]>$arr[$j]){
         
          $min = $j;     
        }

      }
      if($min != $i){

          $temp = $arr[$min];
          $arr[$min] = $arr[$i];
          $arr[$i] = $temp;   
      }

    }
    return $arr;

  }

  $arr = array(2,5,8,4,3,1,6,7,9);

  $old_array = SelectionSort($arr);

print_r($old_array);exit;

Related recommendations:

php algorithm heap sort

php algorithm quick sort


The above is the detailed content of PHP algorithm selection sort. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP algorithm heap sortNext article:PHP algorithm heap sort