Php implements four sorting algorithms _ PHP Tutorial

Source: Internet
Author: User
Php implements four sorting algorithms. Php implements four sorting algorithms. The article comes from "PHP100 文”". prerequisites: use bubble sorting, quick sorting, select sorting, and insert sorting to implement four sorting algorithms from small to php in the following array.

The article is from "PHP100 Chinese network"


Premise: 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 sorting

Train of Thought analysis: In a set of numbers to be sorted, compare and adjust the adjacent two numbers from the past to the next, so that the larger numbers 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 a group 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 a group to be sorted, assuming that the previous number is already sorted, we need to insert the nth number into the previous order number, so that the n numbers are 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 benchmark element, usually 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 );
}


Tips: Use the bubble sort method and quick sort method respectively. select the sort method and insert the sort method to sort the values in the following array from small...

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.