PHP quick sort

巴扎黑
Release: 2016-11-22 11:41:59
Original
1107 people have browsed it

<?php
//Quick Sort
function quickSort(array $array){
  static $run = 0;  
  $len = count($array);
  if($len <= 1) return $array;
  $arrleft=array();
  $arrright=array();
  $flag = $array[0];
  for($i=1;$i<$len;$i++){
 if($array[$i]<=$flag){
$arrleft[]=$array[$i];
 }else{
$arrright[] = $array[$i];
 }
  }
   $arrleft = quickSort($arrleft);
   $arrright = quickSort($arrright);
  $run++;
  return array_merge($arrleft,array($flag),$arrright);
}
$data= array(10,12,43,32,123,12);
$newarr = quickSort($data);
print_r($newarr);
?>
Copy after login

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