PHP four basic Algorithm code example _php tutorial

Source: Internet
Author: User
PHP four basic algorithms: bubbling, selection, insertion and quick Sort method
Many people say that the algorithm is the core of the program, a program is better than poor, the key is the merits of the program algorithm. As a primary phper, although it is seldom exposed to algorithmic aspects of things. But for the bubble sort, insert sort, choose Sort, quick sort four basic algorithms, I think still have to master. Here are my own understanding of the four methods to analyze it again.
Requirements: Using bubble sort method, quick sorting method, selecting Sort method, inserting sorting method to sort the values in the following array in order from small to the next.
$arr (1,43,54,62,21,66,32,78,36,76,39);

1. Bubble Sorting method
* Thinking Analysis: The law, as its name, is like bubbling, each time from the array to take a maximum number out.
* For example: 2,4,1//The First bubble that pops up is 4
* 2,1,4//The Second bubble that pops up is 2
* 1,2,4//The end is like this

* Code implementation:
Copy the Code code as follows:
$arr =array (1,43,54,62,21,66,32,78,36,76,39);
function Getpao ($arr)
{
$len =count ($arr);
Set an empty array to receive the bubbling
The layer loop controls the number of rounds that need to bubble
for ($i =1; $i < $len; $i + +)
{//This layer loop is used to control the number of times a number needs to be compared per 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 sorting:
Choose a sorting method: Select a corresponding element each time, and then place it at the specified location
Copy code code as follows:
function Select_sort ($arr) {
//Realize the idea of double loop completion, outer control wheel count, current minimum value. The number of comparisons of the inner control
//$i The position of the current minimum value, the element to be involved in the comparison
for ($i =0, $len =count ($arr); $i < $len-1; $i + +) {
//First assume the position of the smallest value
$p = $i ; The
//$j currently need to compare with which elements, $i behind.
for ($j = $i +1; $j < $len; $j + +) {
//$arr [$p] is the currently known minimum value
if ($arr [$p] > $arr [$j]) {
//compare, find smaller, record the position of the minimum value; And at the next comparison, the
//should be compared with a known minimum value.
$p = $j;
}
}
//The location of the current minimum value has been determined and saved to $p.
//If the position of the minimum value is found to be different from the current assumed position $i, the position is interchangeable
if ($p! = $i) {
$tmp = $arr [$p];
$arr [$p] = $arr [$i];
$arr [$i] = $tm P
}
}
//Return final result
return $arr;
}

3. Insert Sort method
Insert Sort idea: Inserts the element that will be sorted into the specified position of the array that has assumed the sort number. The
copy Code code is as follows:
function Insert_sort ($arr) {
//distinguish which part is already sorted
//Which part is not sorted
// Find one of the elements that need to be sorted
//This element is starting with the second element, and the last element is the element that needs to be sorted
//The loop can be used to flag the
//i loop control each element that needs to be inserted, once the element that needs to be inserted is controlled,
// Indirectly has divided the array into 2 parts, the subscript is smaller than the current (left), is a sorted sequence
for ($i =1, $len =count ($arr); $i < $len; $i + +) {
//Get the element values that are currently being compared.
$tmp = $arr [$i];
//Inner loop control compares and inserts
for ($j = $i-1; $j >=0; $j-) {
//$arr [$i];//the element that needs to be inserted; $arr [$j];//the element to compare
if ($tmp < $arr [$j]) {
//find the inserted element to be small, swap position
//swap the element behind it with the previous element
$arr [$j +1] = $arr [$j];
//Set the previous number to the number currently required to be swapped
$arr [$j] = $tmp;
} else {
//If you encounter an element that does not need to be moved
//Because the array is already sorted, the preceding one does not need to be compared again.
break;
}
}
}
//Inserts this element into an already sorted sequence.
//return
return $arr;
}

4. Quick Sort Method
Copy the Code code as follows:
function Quick_sort ($arr) {
Determine if you need to proceed
$length = count ($arr);
if ($length <= 1) {
return $arr;
}
If there is no return, the number of elements in the array is 1 extra and needs to be sorted
Select a ruler
Select the first element
$base _num = $arr [0];
Iterates through all elements except the ruler, and fits into two arrays in relation to size
Initialize two arrays
$left _array = Array ();//less than the ruler
$right _array = Array ();//greater than the ruler
for ($i =1; $i < $length; $i + +) {
if ($base _num > $arr [$i]) {
Put in the left array
$left _array[] = $arr [$i];
} else {
Put it on the right
$right _array[] = $arr [$i];
}
}
The same sort method for the left and right arrays, respectively.
Call this function recursively and record the result
$left _array = Quick_sort ($left _array);
$right _array = Quick_sort ($right _array);
Merge left ruler right
Return Array_merge ($left _array, Array ($base _num), $right _array);
}

http://www.bkjia.com/PHPjc/824969.html www.bkjia.com true http://www.bkjia.com/PHPjc/824969.html techarticle PHP four basic algorithms: bubbling, selection, insertion and quick Sort method Many people say that the algorithm is the core of the program, a program is better than poor, the key is the merits of the program algorithm. As a ...

  • 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.