算法

20 阅读1分钟
一、冒泡

<?php

function bubble_sort($arr){

  $count = count($arr);

  if ($count <= 0) return false;

   for($i=0; $i<$count; $i++){

    for($j=$count-1; $j>$i; $j--){

     if ($arr[$j] < $arr[$j-1]){

      $tmp = $arr[$j];

      $arr[$j] = $arr[$j-1];

      $arr[$j-1] = $tmp;

     }

    }

  }

  return $arr;

 }

$_array = array('5', '8' ,'5' ,'6' ,'9' ,'3' ,'2' ,'4');

$_array = bubble_sort($_array);

print_r ($_array);

二、递归

function my_scandir($dir)

{

 $files = array();

 if( $handle = opendir($dir) ){

  while ( ($file = readdir($handle)) !== false ){

   if( $file != ".." && $file != "." ){

    if( is_dir($dir . "/" . $file) ){

     $files[$file] = scandir($dir . "/" . $file);

    }

    else{

     $files[] = $file;

    }