Four basic sorting algorithms in PHP and four sorting algorithms in PHP

Source: Internet
Author: User

Four basic sorting algorithms in PHP and four sorting algorithms in PHP

Eg: bubble sort, quick sort, select sort, insert sort

1. Bubble Sorting

Train of Thought Analysis:In the number group to be sorted, compare and adjust the adjacent two numbers in sequence for the sequence that is not yet sorted, so that the larger number will sink, A relatively small amount of data goes up. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged.

Code implementation:
$ Arr = array (, 78, 39 );
Function bubbleSort ($ arr)
{
$ Len = count ($ arr );
// This layer cyclically controls the number of rounds to be bubbling
For ($ I = 1; $ I <$ len; $ I ++)
{// This layer loop is used to control the number of times a number needs to be compared for each round
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;
}
}
}
Return $ arr;
}

 

2. Select sort

Train of Thought Analysis:In the number of groups to be sorted, select the minimum number and the number at the first position. Then find the smallest number in the remaining number and exchange it with the number in the second position, so that the loop ends until the last number is compared with the last number.

Code implementation:
Function selectSort ($ arr ){
// Dual Loop completion, number of outer control wheels, number of inner control comparisons
$ Len = count ($ arr );
For ($ I = 0; $ I <$ len-1; $ I ++ ){
// First assume the location of the smallest value
$ P = $ I;
For ($ j = $ I + 1; $ j <$ len; $ j ++ ){
// $ Arr [$ p] is the minimum value currently known.
If ($ arr [$ p]> $ arr [$ j]) {
// Compare, find a smaller value, record the location of the minimum value; and use the known minimum value for 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

Train of Thought Analysis:In the number of groups to be sorted, assuming that the previous number is already sorted in order, we need to insert the nth number into the previous ordered number so that the n number is also sorted in order. This repeats until all the rows are sorted.


Code implementation:
Function insertSort ($ arr ){
$ Len = count ($ arr );
For ($ I = 1, $ I <$ len; $ I ++ ){
$ Tmp = $ arr [$ I];
// Loop control in the inner layer, compare and insert
For ($ j = $ I-1; $ j> = 0; $ j --){
If ($ tmp <$ arr [$ j]) {
// It is found that the inserted element is small, and the position of the element is switched.
$ Arr [$ j + 1] = $ arr [$ j];
$ 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;
}
}
}
Return $ arr;
}

 

4. Quick sorting

Train of Thought Analysis:Select a base element. Generally, select the first or last element. After a scan, the columns to be sorted are divided into two parts. One part is smaller than the benchmark element, and the other part is greater than or equal to the benchmark element. At this time, the reference element is in the correct position after sorting, and then uses the same method to recursively sort the divided two parts.

Code implementation:
Function quickSort ($ arr ){
// Determine whether or not to continue
$ Length = count ($ arr );
If ($ length <= 1 ){
Return $ arr;
}
// Select the first element as the benchmark
$ Base_num = $ arr [0];
// Traverse all elements except the ruler and put them in two arrays according to the size relationship
// Initialize two Arrays
$ Left_array = array (); // smaller than the baseline
$ Right_array = array (); // greater than the benchmark
For ($ I = 1; $ I <$ length; $ I ++ ){
If ($ base_num> $ arr [$ I]) {
// Put the array on the left
$ Left_array [] = $ arr [$ I];
} Else {
// Put it on the right
$ Right_array [] = $ arr [$ I];
}
}
// Recursively call this function to sort the array on the left and right respectively in the same way.
$ Left_array = quick_sort ($ left_array );
$ Right_array = quick_sort ($ right_array );
// Merge
Return array_merge ($ left_array, array ($ base_num), $ right_array );
}

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.