Three sorting methods for arrays in PHP and three sorting methods for php Arrays

Source: Internet
Author: User

Three sorting methods for arrays in PHP and three sorting methods for php Arrays
Note: Find the maximum number, arrange it to the end, and continue to find the example: $ arr = array (,-, 2); for ($ I = 0; $ I <count ($ arr)-1; $ I ++) {for ($ j = 0; $ j <count ($ arr)-1-$ I; $ j ++) {if ($ arr [$ j]> $ arr [$ j + 1]) {$ temp = $ arr [$ j]; $ arr [$ j] = $ arr [$ j + 1]; $ arr [$ j + 1] = $ temp ;}} understanding, 2 // compare from the first number to the next. If it is greater than the next number, it is equal to the next number position. // for the first time, 3 is less than 5. Then it remains unchanged. // for the second time, 5 is greater than-1, then it becomes 3,-, // The third time, 5 is greater than 03,-, 5, 2 // the fourth time, 5 is greater than 23, so far, an inner loop is completed. At this time, the last number is sorted and will not participate in 3,-,. The second outer loop starts for the first time: 3 greater than-1, 5 second: 3 is greater than 0-, 3, 2, 5 Third time: 3 is greater than 2-, 2, 3, 5. Now the next two digits are sorted, and so on-, 2, 3, 5. Select the sorting method. Assume that the first number is the smallest number, and then compare the subsequent number with it. If the number is not the smallest, change it to the smallest number behind it. $ arr = array (,-, 0); for ($ I = 0; $ I <count ($ arr) -1; $ I ++) {$ minval = $ arr [$ I]; $ minindex = $ I; for ($ j = 1 + $ I; $ j <count ($ arr); $ j ++) {if ($ arr [$ j] <$ minval) {$ minval = $ arr [$ j]; $ minindex = $ j ;}$ temp = $ arr [$ I]; $ arr [$ I] = $ arr [$ minindex]; $ arr [$ minindex] = $ temp;} Understanding: 2, 1,-, 0 // first assume that the first number 2 is the minimum value, and the number after it is compared with 2 in sequence to find the smallest number process: 1 less than 2, so minval = 1-1 is less than 1, then minval =-13 is greater than-1, unchanged 0 is greater than-1, the minimum number in the array is-1. Change the positions of-1 and 2 to complete the sorting of the first number. Now the array is-, 2, 3, 0 now the first number-1 is already in order, so do not participate in the comparison, continue to the following now assuming minval = 12 is greater than 1, unchanged 3 is greater than 1, unchanged 0 is less than 1, so minval = 0 is now completed in a loop, and the position 0 and 1 is changed to complete the sorting of the second number. Now the array is-, 2, 3, 1 // The method behind the push is the same as above... Iii. Insert sorting method Description: assume that the first number in an array is a separate ordered array, and then the next number and it [here with its I increase, it is changed to them.] For comparison, if the number behind is smaller than the assumed number, move the small number to the beginning, and then move the number to the beginning $ arr = array, -1, 3, 0); for ($ I = 1; $ I <count ($ arr); $ I ++) {$ insertval = $ arr [$ I]; $ insertindex = $ I-1; while ($ insertindex> = 0 & $ insertval <$ arr [$ insertindex]) {$ arr [$ insertindex + 1] = $ arr [$ insertindex]; $ insertindex -- ;}$ temp = $ arr [$ I]; $ arr [$ insertindex + 1] = $ insertval;} Understanding:,-, 0 // for the first time, first save the number 1 To be inserted as insertval And then compare insertval with 2, 1 is less than 2, so move 2 back to figure 2, 2,-1, 3, 0 // at this time there is no number before 2, insertindex = 0, therefore, after comparison, insert insertval to the desired position. Change to 1, 2,-, 0 // at this time, 1, 2 is an ordered array // the second time, first save the number-1 to be inserted as insertval, and then compare insertval with 2, -1 is less than 2, SO 2 is moved back to 1, 2, 2, 3, 0 // at this time, and then compare insertval with 1,-1 is less than 1, then move-1 back, as shown in (this is the process of comparing the number to be inserted with the preceding ordered array), 0 // at this time, insertindex is in the beginning, therefore, insert insertval into the position-, 0 // The following method is used as above.

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.