Home > Backend Development > PHP Tutorial > php bubble sorting code

php bubble sorting code

WBOY
Release: 2016-07-25 08:43:20
Original
930 people have browsed it
  1. function bubbleSort ($items) {
  2. $size = count($items);
  3. for ($i=0; $i<$size; $i++) {
  4. for ($j=0; $j<$size-1-$i; $j++) {
  5. if ($items[$j+1] < $items[$j]) {
  6. arraySwap($items, $j, $j+1);
  7. }
  8. }
  9. }
  10. return $items;
  11. }
  12. function arraySwap (&$arr, $index1, $index2) {
  13. list($arr[$index1], $arr[$index2]) = array($arr[$index2], $arr[$index1]);
  14. }
复制代码

php


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