PHP algorithm selection sort

不言
Release: 2023-03-23 09:48:01
Original
2422 people have browsed it

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!