Home > php教程 > php手册 > Quick sort

Quick sort

WBOY
Release: 2016-08-31 08:40:55
Original
1339 people have browsed it
<?<span style="color: #000000;">php
</span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">快速排序(不要求)//有问题</span>
<span style="color: #800080;">$array</span>=<span style="color: #0000ff;">array</span>(0,5,-1,98,-35<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">动态的创建200000个数据,不要打出,机器会死掉!</span><span style="color: #008000;">
/*</span><span style="color: #008000;">for($i=0;$i<200000;$i++){
      $array[$i]=rand(0,3000);
}
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span> quickSort(<span style="color: #800080;">$left</span>,<span style="color: #800080;">$right</span>,&<span style="color: #800080;">$array</span><span style="color: #000000;">){
   </span><span style="color: #800080;">$l</span>=<span style="color: #800080;">$left</span><span style="color: #000000;">;
   </span><span style="color: #800080;">$r</span>=<span style="color: #800080;">$right</span><span style="color: #000000;">;
   </span><span style="color: #800080;">$pivot</span>=<span style="color: #800080;">$array</span>[(<span style="color: #800080;">$left</span>+<span style="color: #800080;">$right</span>)/2<span style="color: #000000;">];
   </span><span style="color: #800080;">$temp</span>=0<span style="color: #000000;">;
   </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$l</span><<span style="color: #800080;">$r</span><span style="color: #000000;">){
         </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$array</span>[<span style="color: #800080;">$l</span>]<<span style="color: #800080;">$pivot</span>) <span style="color: #800080;">$l</span>++<span style="color: #000000;">;
         </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$array</span>[<span style="color: #800080;">$r</span>]><span style="color: #800080;">$pivot</span>) <span style="color: #800080;">$r</span>--<span style="color: #000000;">;

         </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$l</span>>=<span style="color: #800080;">$r</span>) <span style="color: #0000ff;">break</span><span style="color: #000000;">;

         </span><span style="color: #800080;">$temp</span>=<span style="color: #800080;">$array</span>[<span style="color: #800080;">$l</span><span style="color: #000000;">];
         </span><span style="color: #800080;">$array</span>[<span style="color: #800080;">$l</span>]=<span style="color: #800080;">$array</span>[<span style="color: #800080;">$r</span><span style="color: #000000;">];
         </span><span style="color: #800080;">$array</span>[<span style="color: #800080;">$r</span>]=<span style="color: #800080;">$temp</span><span style="color: #000000;">;

         </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$array</span>[<span style="color: #800080;">$l</span>]==<span style="color: #800080;">$pivot</span>)  --<span style="color: #800080;">$r</span><span style="color: #000000;">;
         </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$array</span>[<span style="color: #800080;">$r</span>]==<span style="color: #800080;">$pivot</span>)  ++<span style="color: #800080;">$l</span><span style="color: #000000;">;
   }
   </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$l</span>==<span style="color: #800080;">$r</span><span style="color: #000000;">){
      </span><span style="color: #800080;">$l</span>++<span style="color: #000000;">;
      </span><span style="color: #800080;">$r</span>--<span style="color: #000000;">;
   }

   </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$left</span><<span style="color: #800080;">$r</span>) quickSort(<span style="color: #800080;">$left</span>,<span style="color: #800080;">$r</span>,<span style="color: #800080;">$array</span><span style="color: #000000;">);
   </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$right</span>><span style="color: #800080;">$l</span>) quickSort(<span style="color: #800080;">$l</span>,<span style="color: #800080;">$right</span>,<span style="color: #800080;">$array</span><span style="color: #000000;">);
}
</span><span style="color: #800080;">$today</span>=<span style="color: #008080;">date</span>('Y-n-d G:i:s'<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> '排序前时间是'.<span style="color: #800080;">$today</span><span style="color: #000000;">;
quickSort(</span>0,<span style="color: #008080;">count</span>(<span style="color: #800080;">$array</span>)-1,<span style="color: #800080;">$array</span><span style="color: #000000;">);
</span><span style="color: #800080;">$today</span>=<span style="color: #008080;">date</span>('Y-n-d G:i:s'<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> '排序后时间是'.<span style="color: #800080;">$today</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br/>";
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template