PHP implementation of sorting algorithms [II]

Source: Internet
Author: User
* ** @ Title Bubble Sorting * @ desc in the number of groups to be sorted, all the numbers within the range not sorted yet, * compare and adjust the adjacent two numbers from top to bottom, so that a large number will sink and a small number will go up. * That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are the opposite, they are exchanged. * @ P

/*** @ Title Bubble Sorting * @ desc in the number of groups to be sorted, all the numbers in the range not sorted yet, * compare and adjust the adjacent two numbers from top to bottom, so that a large number will sink and a small number will go up. * That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are the opposite, they are exchanged. * @ P

/*** @ Title Bubble Sorting * @ desc in the number of groups to be sorted, all the numbers in the range not sorted yet, * compare and adjust the adjacent two numbers from top to bottom, so that a large number will sink and a small number will go up. * That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are the opposite, they are exchanged. * @ Param array $ data * @ return array $ data * @ author bandit */function bubble_sort (array $ data) {$ length = count ($ data ); for ($ I = $ length; $ I> 1; $ I --) {for ($ j = 0; $ j <$ I-1; $ j ++) {if ($ data [$ j]> $ data [$ I + 1]) {list ($ data [$ I], $ data [$ j]) = array ($ data [$ j], $ data [$ j + 1]) ;}}return $ data ;} /*** @ ttile quick sorting * @ desc * @ param array $ data ** @ return array $ data * @ author bandit */function quick_sort ($ data) {$ front = $ end = array (); $ length = count ($ data); if ($ length <= 1) {return $ data ;} for ($ I = 1; $ I <$ length; $ I ++) {if ($ data [$ I] <= $ data [0]) {$ front [] = $ data [$ I]; unset ($ data [$ I]);} else {array_unshift ($ end, $ data [$ I]); unset ($ data [$ I]); }}$ front = quick_sort ($ front); $ end = quick_sort ($ end); return array_merge ($ front, array ($ data [0]), $ end );}

Summary of the stability, time complexity, and space complexity of various sorting:

Increase of time complexity function O (n)

Reference: http://blog.csdn.net/hguisu/article/details/7776068

Original article address: Sorting Algorithm PHP implementation [below]. Thanks to the original author for sharing.

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.