PHP sorting algorithm (insert, select, switch, bubble, fast)

Source: Internet
Author: User
PHP sorting algorithm (insert, select, switch, bubble, fast) & lt ;? Php bubble sorting functionBubbleSort ($ arr) {get the total length of the array $ numcount ($ arr); forward traversing array for ($ i1; $ I & lt; $ num; $ I ++) {reverse query PHP sorting algorithm (insert, select, exchange, bubble, fast)
 = $ I; $ j --) {// compare two adjacent numbers if ($ arr [$ j] <$ arr [$ j-1]) {// save a smaller number $ iTemp = $ arr [$ j-1]; // put a larger front $ arr [$ j-1] = $ arr [$ j]; // Put a small value behind $ arr [$ j] = $ iTemp ;}}return $ arr;} // sort function ExchangeSort ($ arr) by the exchange method) {$ num = count ($ arr); // traverse the array for ($ I = 0; $ I <$ num-1; $ I ++) {// Obtain the next index of the current index for ($ j = $ I + 1; $ j <$ num; $ j ++) {// compare the values of the adjacent two if ($ arr [$ j] <$ arr [$ I]) {// A small number of temporary stores $ iTemp = $ arr [$ I]; // Put a large value in front $ Arr [$ I] = $ arr [$ j]; // add a small value to the back of $ arr [$ j] = $ iTemp ;}}return $ arr ;} // sort function SelectSort ($ arr) {// obtain the total length of the array $ num = count ($ arr); // traverse the array for ($ I = 0; $ I <$ num-1; $ I ++) {// Save current value $ iTemp = $ arr [$ I]; // Save current location $ iPos = $ I; // traverse the data after the current position for ($ j = $ I + 1; $ j <$ num; $ j ++) {// if there is an if ($ arr [$ j] <$ iTemp) smaller than the current value {// minimum value for temporary storage $ iTemp = $ arr [$ j]; // saved location $ iPos = $ j ;}// place the current value to the calculated location $ arr [$ iPos] = $ ar R [$ I]; // replace the current value with the calculated value $ arr [$ I] = $ iTemp;} return $ arr ;} // Insert sort function InsertSort ($ arr) {$ num = count ($ arr); // traverse the array for ($ I = 1; $ I <$ num; $ I ++) {// Obtain the current value $ iTemp = $ arr [$ I]; // Obtain the previous position of the current value $ iPos = $ I-1; // if the current value is less than the previous value, it is not switched to the starting position of the array while ($ iPos >=0) & ($ iTemp <$ arr [$ iPos]) {// put the previous value back in a position $ arr [$ iPos + 1] = $ arr [$ iPos]; // The position is decreasing by $ iPos --;} $ arr [$ iPos + 1] = $ iTemp;} return $ arr;} // Quick sort function QuickSort ($ arr) {$ num = count ($ arr); $ l = $ r = 0; $ left = $ right = array (); // traverse the array from the second index ($ I = 1; $ I <$ num; $ I ++) {// if the value is less than index 1 if ($ arr [$ I] <$ arr [0]) {// load the left index array (data smaller than index 1) $ left [] = $ arr [$ I]; $ l ++;} else {// otherwise, load the data larger than index 1 into the right index) $ right [] = $ arr [$ I]; $ r ++; //} // if the left index has a value, sort the left index if ($ l> 1) {$ left = QuickSort ($ left);} // sorted array $ new_arr = $ left; // put the first current array to the end $ new_arr [] = $ arr [0]; // If another index has a value, sort the right index. if ($ r> 1) {$ right = QuickSort ($ right );} // add Data for ($ I = 0; $ I <$ r; $ I ++) again based on the length of the right index) {$ new_arr [] = $ right [$ I];} return $ new_arr;}?>

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.