Four basic algorithms of php

Keywords: PHP less

Four basic algorithms of php
Many people say that the algorithm is the core of the program, a program is better than bad, the key is the quality of the program algorithm. As a phper, it has little exposure to algorithmic aspects. But for the four basic algorithms of bubble sort, insert sort, select sort and quick sort, I think we still need to master them. Below is my own understanding of the four methods will be analyzed once. Requirements: Bubble sorting, selection sorting, insertion sorting and quick sorting are used to sort the values of the following arrays in order from small to small. $arr(1,43,54,62,21,66,32,78,36,76,39);
1. Bubble Sorting Method
[PHP] Pure Text View Copy Code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/*
* Idea analysis: As its name implies, it is like bubbles, which emit the largest number from an array at a time.

  • For example: 2,4,1// The first bubble is 4.
  • 2,1,4// The second bubble is 2
  • 1,2,4// That's how it ends up.

*/
function bubble_sort($arr){

$len=count($arr);
//Set up an empty array to receive bubbles that pop up
//The number of bubbling rounds required for this layer of cyclic control
for($i=1;$i<$len-1;$i++)
{ //This layer loop is used to control the number of times a number needs to be compared in 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;

}
$arr=array(1,43,54,62,21,66,32,78,36,76,39);
echo '<pre>';
print_r( bubble_sort($arr) );
?>

Design sketch:

2. Selective Sorting Method
[PHP] Pure Text View Copy Code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
// Choose Sort Method: Select a corresponding element at a time, and then place it in the specified location.
function select_sort($arr) {
// The realization of the dual cycle of ideas, the number of outer control wheels, the current minimum. Number of comparisons of internal controls

//The location of the current minimum of $i, the elements that need to be compared
for($i=0, $len=count($arr); $i<$len-1; $i++) {
    //Let's first assume the location of the smallest value.
    $p = $i;
    //Which elements do $j currently need to be compared with, $i is behind.
    for($j=$i+1; $j<$len; $j++) {
        //$arr[$p] is the current known minimum
        if($arr[$p] > $arr[$j]) {
                         //Compare, find smaller, record the location of the minimum; and in the next comparison,
                             // Comparisons should be made with known minimum values.
            $p = $j;
        }
    }
    //The location of the current minimum has been determined and saved to $p.
             //If the location of the minimum is found to be different from the current hypothetical location of $i, then the position can be exchanged.
    if($p != $i) {
        $tmp = $arr[$p];
        $arr[$p] = $arr[$i];
        $arr[$i] = $tmp;
    }
}
//Return the final result
return $arr;

}
$arr=array(1,43,54,62,21,66,32,78,36,76,39);
echo '<pre>';
print_r( select_sort($arr) );
?>

Design sketch:

3. Insertion sort method
[PHP] Pure Text View Copy Code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
// The idea of insertion sort method: insert the sorted elements into the specified position of the array that has assumed the sorting number.
function insert_sort($arr) {

  //Distinguish which parts have been sorted
  //Which parts are not sorted?
  //Find one of the elements to sort
  //This element starts with the second element and ends with the element that needs to be sorted.
  //Use the cycle to mark it.
  //i loop controls the elements that need to be inserted each time, once the elements that need to be inserted are well controlled.
  //Indirectly, the array has been divided into two parts, the subscript is less than the current (left), is a sorted sequence.
  for($i=1, $len=count($arr); $i<$len; $i++) {
      //Get the current element values that need to be compared.
      $tmp = $arr[$i];
      //Inner loop control comparison and insertion
      for($j=$i-1;$j>=0;$j--) {
      //$arr[$i]; // elements to be inserted; $arr[$j]; // elements to be compared
          if($tmp < $arr[$j]) {
              //Find that the inserted elements are smaller and swap positions
              //Exchange the elements behind with the elements ahead
              $arr[$j+1] = $arr[$j];
              //Set the previous number to the current number to be exchanged
              $arr[$j] = $tmp;
          } else {
              //If you encounter elements that do not need to be moved
              //Since the arrays have been sorted, there is no need to compare them again.
              break;
          }
      }
  }
  //Insert this element into the sorted sequence. Return
  return $arr;

}
$arr=array(1,43,54,62,21,66,32,78,36,76,39);
echo '<pre>';
print_r( insert_sort($arr) );
?>

Design sketch:

4. Quick Sorting Method
[PHP] Pure Text View Copy Code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
function quick_sort($arr) {

//Judge first whether it is necessary to proceed
$length = count($arr);
if($length <= 1) {
    return $arr;
}
//If there is no return, it means that the number of elements in the array is one more and needs to be sorted.
//Choose a ruler
//Select the first element
$base_num = $arr[0];
//Traverse all elements except rulers and put them into two arrays according to size
//Initialize two arrays
$left_array = array();//Less than a ruler
$right_array = array();//Larger than a ruler
for($i=1; $i<$length; $i++) {
    if($base_num > $arr[$i]) {
        //Put it in the left array
        $left_array[] = $arr[$i];
    } else {
        //Put on the right side
        $right_array[] = $arr[$i];
    }
}
//Then the left and right arrays are sorted in the same way.
//Call this function recursively and record the results
$left_array = quick_sort($left_array);
$right_array = quick_sort($right_array);
//Merge left ruler and right ruler
return array_merge($left_array, array($base_num), $right_array);

}
?>

Design sketch:


More technical information: gzitcast

Posted by chowwiie on Mon, 16 Sep 2019 02:13:32 -0700