Home >Backend Development >PHP Tutorial >PHP selection sorting algorithm implementation code

PHP selection sorting algorithm implementation code

WBOY
WBOYOriginal
2016-07-25 08:54:151303browse
  1. //Select sort function function
  2. //Use references to reduce memory consumption
  3. //Organization: bbs.it-home.org
  4. function &select(&$arr){
  5. $count= count($arr);
  6. if($count>1){
  7. for($i=0;$i<$count-1;$i++){
  8. $k=$i;
  9. for($j=$i +1;$j<$count;$j++){
  10. if($arr[$j]<$arr[$k]){
  11. $k=$j;
  12. }
  13. }
  14. if($k!= $i){
  15. $tmp=$arr[$k];
  16. $arr[$k]=$arr[$i];
  17. $arr[$i]=$tmp;
  18. }
  19. }
  20. }
  21. return $ arr;
  22. }
  23. //Print array function
  24. function printArr(&$arr){
  25. echo "
    "; </li>
    <li> print_r($arr); </li>
    <li> echo "
    ";
  26. }
  27. //Start testing
  28. //Assign ten random values ​​to the array
  29. for($i=0;$i<10;$i++){
  30. $testArr[]=rand(10,100);
  31. }
  32. printArr(select($ testArr));
  33. ?>
Copy code

>>> Articles you may be interested in: PHP practical quick sorting algorithm example code A summary of the implementation of various sorting algorithms in PHP Small example of php bubble sort A simple example of implementing bubble sort in php PHP two-dimensional array sorting custom function php selection sorting implementation code php bubble sort implementation code php insertion sort implementation code php function to implement quick sort php function to implement quick sort php bubble sort exchange sort method Example of php bubble sort php code to implement bubble sort algorithm An example of php bubble sorting algorithm Examples of php bubble sort and quick sort Two methods of php two-dimensional array sorting php multidimensional array sorting



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