Four basic php Algorithms: Bubble, selection, insertion, and quick sorting

Source: Internet
Author: User
Many people say that an algorithm is the core of a program. if a program is better or worse, the key is its advantages and disadvantages. As a beginner phper, although seldom touched on algorithms. However, for bubble sorting, Insertion sorting, selection sorting, and quick sorting

Many people say that an algorithm is the core of a program. if a program is better or worse, the key is its advantages and disadvantages. As a beginner phper, although seldom touched on algorithms. However, I still want to master the four basic algorithms for bubble sorting, insert sorting, select sorting, and fast sorting. The following is an analysis of the four methods.

Requirement: use the bubble sorting method and the quick sorting method respectively. select the sorting method and insert the sorting method to sort the values in the following array in the ascending order. $ Arr (, 78, 39); * 1. bubble sort method * train of thought analysis: the method, as its name is, is like a bubble, each time from the array out of a maximum number. * For example, the first bubble of 2, 4, 1 // is 4*2, 1, 4 // The second bubble is 2*2, 2, 4 //.

* Code implementation:

$ 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 ;}

2. select the sorting method:

Sort method: select an element each time and place it in the specified position.

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 ;}

3. Insert sorting

Insert sorting method: Insert the elements to be sorted to the specified position of the array with the assumed order number.

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 ;}

4. quick sorting


Function quick_sort ($ arr) {// first judge whether to continue $ length = count ($ arr); if ($ length <= 1) {return $ arr ;} // if no result is returned, the number of elements in the array is more than one. sort the result. // select a ruler. // select the first element $ base_num = $ arr [0]. // traverse all elements except the ruler and put them into two arrays according to the size relationship. // Initialize two arrays $ left_array = array (); // $ right_array = array (); // for ($ I = 1; $ I <$ length; $ I ++) smaller than the ruler) {if ($ base_num> $ arr [$ I]) {// put the left array $ left_array [] = $ arr [$ I];} else {// Put $ right_array [] = $ arr [$ I];} on the right // perform the same sorting method for the array on the left and right respectively. // call this function recursively and record the result $ left_array = quick_sort ($ left_array ); $ right_array = quick_sort ($ right_array); // merge return array_merge ($ left_array, array ($ base_num), $ right_array) on the right of the left ruler );}

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.