How to sort arrays in PHP

Source: Internet
Author: User
Three sorting methods of arrays in PHP, including bubble sorting, selection sorting, and insertion sorting. For more information, see I. Bubble sorting
Note: find the maximum number, arrange it to the last side, and continue to find it.

Example:

The code is as follows:


$ Arr = array (3,5,-1,0, 2 );
For ($ I = 0; $ I
For ($ j = 0; $ j
If ($ arr [$ j]> $ arr [$ j + 1]) {
$ Temp = $ arr [$ j];
$ Arr [$ j] = $ arr [$ j + 1];
$ Arr [$ j + 1] = $ temp;
}
}
}


Understanding:
3, 5,-1, 0, 2
// Compare the values starting from the first number.
// For the first time, 3 is less than 5, so it remains unchanged
// For the second time, if 5 is greater than-1
3 ,-,
// The third time, 5 is greater than 0
3,-1, 0, 5, 2
// The fourth time, 5 is greater than 2
3,-1, 0, 2, 5
So far, an internal loop is completed, and the last number is sorted.
3,-, the second external loop starts the first time: 3 is greater than-1
-1, 3, 0, 2, 5
Second: 3 is greater than 0
-1, 0, 3, 2, 5
Third time: 3 is greater than 2
-1, 0, 2, 3, 5
So far, the sorting of the next two digits is completed, and so on.
-1, 0, 2, 3, 5
2. select sorting method
Note: assume that the first number is the smallest number, and then compare the subsequent number with it in sequence. if the number is not the smallest, change it to the smallest number.

The code is as follows:


$ Arr = array (2, 1,-1, 3, 0 );
For ($ I = 0; $ I
$ Minval = $ arr [$ I];
$ Minindex = $ I;
For ($ j = 1 + $ I; $ j
If ($ arr [$ j] <$ minval ){
$ Minval = $ arr [$ j];
$ Minindex = $ j;
}
}
$ Temp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ minindex];
$ Arr [$ minindex] = $ temp;
}


Understanding:
2, 1,-1, 3, 0
// Assume that the first number 2 is the minimum value, and the number after it is compared with 2 in sequence to find the minimum number
Process:
1 is less than 2, so minval = 1
-1 is less than 1, then minval =-1
3 is greater than-1, unchanged
0 is greater than-1, unchanged
Now, the minimum number in the array is-1.
Change the positions of-1 and 2 to sort the first number.
Now the array is changed
-1, 1, 2, 3, 0
Now the first number-1 is in order, so we do not participate in the comparison.
Now suppose minval = 1
2 is greater than 1, unchanged
3 is greater than 1, unchanged
If 0 is less than 1, minval = 0
Now a loop is completed, and the second number is sorted by changing the positions 0 and 1.
Now the array is changed
-1, 0, 2, 3, 1
// The following method is the same as above...

III. Insert sorting

Note: assume that the first number in an array is a separate ordered array, and then compare the subsequent number with it [here as it grows, it becomes them, if the subsequent number is smaller than the assumed number, move the smaller one to the beginning

The code is as follows:


$ Arr = array (2, 1,-1, 3, 0 );
For ($ I = 1; $ 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:
2, 1,-1, 3, 0
// For the first time, first save the number 1 to be inserted as insertval, and then compare insertval with 2. 1 is smaller than 2, so move 2 back to the following figure.
2, 2,-1, 3, 0
// At this time, there is no number before 2, insertindex = 0, so the comparison is complete, then insert insertval to the position found. Change to as shown in figure
1, 2,-1, 3, 0
// At this time, 1 or 2 is an ordered array
// For the second time, first save the number to be inserted-1 as insertval, then compare insertval with 2,-1 is smaller than 2, so move 2 back, as shown in
1, 2, 3, 0
// At this time, compare insertval with 1. if-1 is smaller than 1, move-1 back and change it to an example (this is a process of comparing the number to be inserted with the preceding ordered array)
1, 1, 2, 3, 0
// At this time, insertindex is in the beginning, so insert insertval into this position
-1, 1, 2, 3, 0
// The Push method is as follows:

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.