Basic Algorithms: Personal PHP implementation (2)

Source: Internet
Author: User
<? PHP  /*  ** PHP quick sorting  */  $ Arr = Array (,) );  Function Quick ( $ Array  ){  If ( Count ( $ Array ) <= 1) Return   $ Array ;  $ K = $ Array [0 ];  $ Left = Array  ();  $ Right = Array  ();  For ( $ I = 1; $ I < Count ( $ Array ); $ I ++ ){  If ( $ Array [ $ I ] <= $ K  ){  $ Left [] = $ Array [ $ I  ];}  Else  {  $ Right [] =$ Array [ $ I  ] ;}}  $ Left = Quick ( $ Left  );  $ Right = Quick ( $ Right  );  Return   Array_merge ( $ Left , Array ( $ K ),$ Right  );}  Print_r (Quick ( $ Arr  )); ?>

The core idea is to obtain the first element of the array to be sorted each time. If the value is greater than other elements in the array, it is placed on the left or on the right.

Then, the set on the left is sorted in the same order until only 0 ~ exists on the left ~ 1 element. (0 indicates that the first element of this sorting is the smallest. 1 indicates that only one element is greater than others)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.