Four basic php sorting algorithms: php sorting algorithms

Source: Internet
Author: User

Four basic php sorting algorithms: php sorting algorithms
Bubble Sorting

Train of Thought Analysis: Like its name, it is like a bubble, and each time it comes out of the array with the largest number.

First round: from the first to the last bubble comparison, run the result: the last maximum

Second round: from the first to the second to the last bubble comparison, run the result: the last maximum (the last one of the current round)

And so on...

$ Arr = array (, 54,62, 66, 39); function getpao ($ arr) {$ len = count ($ arr ); // set an empty array to receive bubbles. // This layer cyclically controls the number of wheels to be bubble ($ I = 1; $ I <$ len; $ I ++) {// This layer loop is used to control the number of times a number needs to be compared in each round ($ k = 0; $ k <$ len-$ I; $ k ++) {if ($ arr [$ k]> $ arr [$ k + 1]) {$ tmp = $ arr [$ k + 1]; $ arr [$ k + 1] = $ arr [$ k]; $ arr [$ k] = $ tmp ;}} return $ arr ;}

 

Select sort

Train of Thought Analysis: select a corresponding element each time and place it in the specified position

First round: Start from the first place and place the smallest element in the first place (compare in sequence, store the smaller subscript to $ p, and then exchange with the current position element)

The second round: starting from the second digit (the first digit is already the smallest number), and selecting the smallest element to the first (the second digit of the entire array)

And so on...

Function select_sort ($ arr) {// implement the concept of Dual Loop completion, the number of outer control wheels, the current minimum value. Number of comparisons controlled by the inner layer // the position of the current minimum value of $ I. The element to be involved in the comparison is for ($ I = 0, $ len = count ($ arr ); $ I <$ len-1; $ I ++) {// first assume the location of the smallest value $ p = $ I; // What elements do j need to compare with currently, $ I. For ($ j = $ I + 1; $ j <$ len; $ j ++) {// $ arr [$ p] is the currently known minimum value. if ($ arr [$ p]> $ arr [$ j]) {// comparison, it is found that, record the location of the minimum value, and compare it with the known minimum value in the next comparison. $ P = $ j ;}/// the location of the current minimum value has been determined and saved to $ p. // If you find that the location of the minimum value is different from the current position $ I, you can change the location if ($ p! = $ I) {$ tmp = $ arr [$ p]; $ arr [$ p] = $ arr [$ I]; $ arr [$ I] = $ tmp;} // return the final result return $ arr ;}

 

Insert sort

Train of Thought Analysis: insert the elements to be sorted to the specified position of the array already assumed the order number.

First round: Compare the second element with the first element. If it is small, place them in different places.

Second round: Compare the third element with the second element. If the value is small, the position of the Third and Second elements is swapped;

Compare the second element after interchange with the first element. If it is small, the position of the second first element is swapped.

After two rounds, the size of the first three elements has been arranged, and the following process is followed by analogy. Each round is compared with all the preceding elements. If it is not smaller than the preceding element, it does not move; if it is smaller than the previous element, it is exchanged immediately.

Function insert_sort ($ arr) {// identify which part is sorted // which part is not sorted // find one of the elements to be sorted // This element starts from the second element, the last element is the element to be sorted. // The elements to be inserted can be marked by loops. // The I loop controls the elements to be inserted each time. Once the elements to be inserted are well controlled, // indirectly, the array has been divided into two parts. The subscript is smaller than the current one (on the left) and is the sorted sequence for ($ I = 1, $ len = count ($ arr ); $ I <$ len; $ I ++) {// obtain the element value to be compared. $ Tmp = $ arr [$ I]; // compare and insert for inner loop control ($ j = $ I-1; $ j >=0; $ j --) {// $ arr [$ I]; // elements to be inserted; $ arr [$ j]; // The element to be compared if ($ tmp <$ arr [$ j]) {// It is found that the inserted element is small, swap location // swap the following element with the preceding element $ arr [$ j + 1] = $ arr [$ j]; // set the preceding number to the number to be exchanged currently $ arr [$ j] = $ tmp ;} else {// if you encounter elements that do not need to be moved // because they are sorted as arrays, you do not need to compare them again. Break ;}}// insert this element into the sorted sequence. // Return $ arr ;}

 

Quick sorting

Train of Thought Analysis: Take the first element as the reference number, and then set two arrays respectively, put those smaller than the reference number in the left array; put those greater than the reference number in the right array. And so on... (recursion)

1. Find a reference element;

2. Compare all other elements in the array;

3. Place the result after comparison with the reference element into two different arrays based on the size;

4. recursively call the function itself using arrays;

5. Return a sorted array (array_merge ())

Recursive point: Array

Recursive Exit: When the array element has only one (or is empty)

<? Php // quick sorting // The array to be sorted $ arr = array (6, 3, 8, 6, 4, 2, 9, 5, 1); // The function implements quick sorting function quick_sort ($ arr) {// determine whether the parameter is an array if (! Is_array ($ arr) return false; // recursive Exit: if the array length is 1, the returned array $ length = count ($ arr); if ($ length <= 1) return $ arr; // The array element has multiple $ left = $ right = array (); // use the for loop for traversal, use the first element as the comparison object for ($ I = 1; $ I <$ length; $ I ++) {// determine the size of the current element value if ($ arr [$ I] <$ arr [0]) {// The current element is smaller than the standard element, put the array on the left $ left [] = $ arr [$ I];} else {$ right [] = $ arr [$ I] ;}} // recursively call $ left = quick_sort ($ left); $ right = quick_sort ($ right); // merge all results into return array_merge ($ left, array ($ arr [0]), $ right);} echo '<pre>'; // call print_r (quick_sort ($ arr ));

Source: [http://www.cnblogs.com/hf8051/p/4667455.html] Thank you for sharing your thoughts!

Related Article

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.