Four kinds of sorting algorithms in PHP

Source: Internet
Author: User

1 1. Bubble sort2 3 Thinking Analysis: In order to sort a group of numbers, the current is not well-arranged sequence, from the back to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are ordered in the opposite order as they are ordered, they are interchanged. 4 5 Code implementation:6 7 8$arr =array (1, +, Wu, +, +, the, +, +, $, the, the); 9 function Bubblesort ($arr)Ten {  One$len =count ($arr); A   //the layer loop controls the number of rounds that need to bubble -    for($i =1; $i < $len; $i + +) -{//This layer loop is used to control how many times a number needs to be compared per round the      for($k =0; $k < $len-$i; $k + +) -     { -        if($arr [$k]> $arr [$k +1]) -         { +$tmp = $arr [$k +1]; -$arr [$k +1]=$arr [$k]; +$arr [$k]=$tmp; A         } at     } -   } -   return$arr; - } -   -  in 2. Select Sort -  to Code implementation: +  -  the  * function Selectsort ($arr) { $ //double loop complete, outer control wheel count, inner control comparison numberPanax Notoginseng$len =count ($arr); -      for($i =0; $i < $len-1; $i + +) { the         //first assume the position of the smallest value +$p =$i; A           the          for($j = $i +1; $j < $len; $j + +) { +             //$arr [$p] is the currently known minimum value -             if($arr [$p] >$arr [$j]) { $             //Compare, find smaller, record the position of the minimum value, and compare with the known minimum value at the next comparison.  $$p =$j; -             } -         } the         //The location of the current minimum value has been determined and saved to $p. If the location of the minimum value is found to be different from the current assumed position $i, the position is interchangeable.  -         if($p! =$i) {Wuyi$tmp =$arr [$p]; the$arr [$p] =$arr [$i]; -$arr [$i] =$tmp; Wu         } -     } About     //return to final result $     return$arr; - } -   - 3. Insert Sort A  + Thinking Analysis: In order to sort a group of numbers, assuming that the previous number is already in the sequence, now the nth number is inserted into the ordinal number in front, so that the number of n is also in order. This cycle is repeated until all the rows are in order.  the  - Code implementation: $  the  the  the function Insertsort ($arr) { the$len =count ($arr); -      for($i =1, $i < $len; $i + +) { in$tmp =$arr [$i]; the         //Inner loop control, compare and insert the          for($j = $i-1; $j >=0; $j--) { About             if($tmp <$arr [$j]) { the                 //find the inserted element to be small, swap position, swap the element behind with the previous element the$arr [$j +1] =$arr [$j]; the$arr [$j] =$tmp; +}Else { -                 //If you encounter an element that does not need to be moved, because the array is already sorted, the preceding one does not need to be compared again.  the                  Break;Bayi             } the         } the     } -     return$arr; - } the   the 4. Quick Sort the  the Thinking Analysis: Select a datum element, usually select the first element or the last element. With a scan, the pending sequence is divided into two parts, some smaller than the Datum element, and a portion greater than or equal to the datum element. At this point the datum element is in the correct position after it is ordered, and then the same method is used to sort the two parts recursively.  -  the Code implementation: the  the 94  the function QuickSort ($arr) { the     //determine if you need to proceed the$length =count ($arr);98     if($length <=1) { About         return$arr; -     }101     //Select the first element as a datum102$base _num = $arr [0];103     //iterates through all elements except the ruler, and fits into two arrays in relation to size104     //Initialize two arrays the$left _array = Array ();//less than the baseline106$right _array = Array ();//greater than the baseline107      for($i =1; $i < $length; $i + +) {108         if($base _num >$arr [$i]) {109             //put in the left array the$left _array[] =$arr [$i];111}Else { the             //put it on the right113$right _array[] =$arr [$i]; the         } the     } the     //This function is recursively called on the left and right arrays by the same sort method.117$left _array =Quick_sort ($left _array);118$right _array =Quick_sort ($right _array);119     //Merging -     returnArray_merge ($left _array, Array ($base _num), $right _array);121 }122  

Four kinds of sorting algorithms in PHP

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.