Four basic php algorithm code instances-php instances

Source: Internet
Author: User
Algorithms are the core of the program. today we will learn four basic php Algorithms: Bubble, selection, insertion, and fast sorting. Four basic php Algorithms: Bubble, selection, insertion, 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 sorting
* Train of thought Analysis: like its name, it is like a bubble. each time it comes out of the array with the largest number.
* For example: 2, 4, 1 // The first bubble popped up is 4
* 2, 1, 4 // The second bubble popped up is 2
* 1, 2, 4 // This is the final result

* Code implementation:

The code is as follows:


$ Arr = array (, 78, 39 );
Function getpao ($ arr)
{
$ Len = count ($ arr );
// Set an empty array to receive the bubble
// 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 the sorting method:
Sort method: select an element each time and place it in the specified position.

The code is as follows:


Function select_sort ($ arr ){
// Implement the concept of dual loop completion, the number of outer control wheels, the current minimum value. Number of inner-layer comparisons
// $ I the position of the current minimum value and the elements to be compared
For ($ I = 0, $ len = count ($ arr); $ I <$ len-1; $ I ++ ){
// First assume the location of the smallest value
$ P = $ I;
// $ J which elements need to be compared with at present.
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 at the next comparison,
// Compare the values with known minimum values.
$ 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.

The code is as follows:


Function insert_sort ($ arr ){
// Identify which part has been sorted
// Which part is not sorted
// Find one of the elements to be sorted
// This element starts from the second element and ends with the last element.
// Use the loop to mark it
// I loop controls the elements to be inserted each time. Once the elements to be inserted are well controlled,
// The array has been indirectly divided into two parts. the subscript is smaller than the current (left) and is a sorted sequence.
For ($ I = 1, $ len = count ($ arr); $ I <$ len; $ I ++ ){
// Obtain the element values to be compared.
$ Tmp = $ arr [$ I];
// Compare and insert the inner loop control
For ($ j = $ I-1; $ j> = 0; $ j --){
// $ Arr [$ I]; // elements to be inserted; $ arr [$ j]; // elements to be compared
If ($ tmp <$ arr [$ j]) {
// It is found that the inserted element is small and the switch position is
// Swap the following elements with the preceding elements
$ Arr [$ j + 1] = $ arr [$ j];
// Set the preceding number to the number to be exchanged
$ Arr [$ j] = $ tmp;
} Else {
// If you encounter elements that do not need to be moved
// Because it is an array that has been sorted, you do not need to compare it again.
Break;
}
}
}
// Insert this element into the sorted sequence.
// Return
Return $ arr;
}

4. quick sorting

The code is as follows:


Function quick_sort ($ arr ){
// Determine whether or not to continue
$ Length = count ($ arr );
If ($ length <= 1 ){
Return $ arr;
}
// If no result is returned, it indicates that the number of elements in the array is more than one and needs to be sorted.
// Select a ruler
// Select the first element
$ 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 ruler's
$ Right_array = array (); // greater than the ruler's
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];
}
}
// Sort 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 the ruler on the left to the right
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.