Home > Backend Development > PHP Tutorial > Implementation of selection sort in PHP

Implementation of selection sort in PHP

小云云
Release: 2023-03-21 12:08:01
Original
1396 people have browsed it

This article mainly introduces to you the implementation of PHP selection sorting. The principle is: each traversal selects the smallest one on the right and exchanges its position with the one on the left. For example: in the first pass, select the first element 4 as a reference, find the right side of 4, and exchange the smallest number with 4 to become: 0, 1,8,4,6,7,2,9,3,200,0,656,5,12,12123,34,9,

<?php 
$arr = [4,1,8,0,6,7,2,9,3,200,0,656,5,12,12123,34,9];
$n = count($arr);
$t = null;
$min =null;
for ($i=0;$i<$n;$i++){	$min = $i;	
for($j=$i+1;$j<$n;$j++){		
if($arr[$min]>$arr[$j]){			
$min= $j;		}	}	
if($min!=$i){	    $t = $arr[$i];		
$arr[$i] = $arr[$min];		$arr[$min] = $t;	
}		foreach($arr as $v){		
echo $v.",";	}		
echo "<br>";
}
?>
Copy after login

Related recommendations:

PHP simple selection sorting algorithm learning and sharing

Instance analysis of JavaScript implementation of selection sorting algorithm (picture)

php simple sorting bubble sort and selection sort

The above is the detailed content of Implementation of selection sort in PHP. 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