A simple quick sort in PHP

WBOY
Release: 2016-07-25 08:42:38
Original
782 people have browsed it
Quick sorting is achieved by continuously locating the position of the reference number
  1. /**
  2. * Created by PhpStorm.
  3. * User: saint
  4. * Date: 15/8/5
  5. * Time: 上午11:49
  6. */
  7. class Demo
  8. {
  9. public $a = array(3, 6, 9, 2, 4 , 7, 1, 5, 8, 0);
  10. public function qsort($left, $right)
  11. {
  12. if($left > $right) {
  13. return;
  14. }
  15. $i = $left;
  16. $j = $right;
  17. $standard = $this->a[$left];
  18. while($i != $j) {
  19. // Find units smaller than the base number from right to left
  20. while(($standard <= $this->a[$j]) && ($j > $i)) {
  21. $j--;
  22. }
  23. // Find the standard number from left to right Big
  24. while(($standard >= $this->a[$i]) && ($j > $i)) {
  25. $i++;
  26. }
  27. $tmp = $this->a [$i];
  28. $this->a[$i] = $this->a[$j];
  29. $this->a[$j] = $tmp;
  30. }
  31. // OK The position of the base number
  32. $this->a[$left] = $this->a[$i];
  33. $this->a[$i] = $standard;
  34. $this->qsort ($left, $i - 1);
  35. $this->qsort($i + 1, $right);
  36. }
  37. // Execute function
  38. public function main()
  39. {
  40. $left = 0;
  41. $right = count($this->a) - 1;
  42. $this->qsort($left, $right);
  43. print_r($this->a);
  44. }
  45. }
  46. $demo = new Demo();
  47. $demo->main();
Copy code

From: http://my.oschina.net/liuke1556/blog/488215
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!