PHP four basic algorithm code examples_PHP tutorial
PHP’s four basic algorithms: bubble, selection, insertion and quick sort
Many people say that the algorithm is the core of the program. The key to a program being better than worse is the quality of the algorithm. As a junior PHPer, although I have little exposure to algorithmic things. But I think we still need to master the four basic algorithms of bubble sort, insertion sort, selection sort, and quick sort. Below is my analysis of the four methods according to my own understanding.
Requirements: Use bubble sort, quick sort, selection sort, and insertion sort to sort the values in the array below in ascending order.
$arr(1,43,54,62,21,66,32,78,36,76,39);
1. Bubble sorting method
* Idea analysis: The method is as its name suggests, just like bubbling, the largest number emerges from the array each time.
* For example: 2,4,1 // The first bubble is 4
* 2,1,4 // The second bubble is 2
* 1,2, 4 // It ended up like this
* Code implementation:
$arr=array(1,43,54, 62,21,66,32,78,36,76,39);
function getpao($arr)
{
$len=count($arr);
//Set an empty The array is used to receive bubbles that pop up
//This layer loop controls the number of rounds of bubbles required
for($i=1;$i<$len;$i++)
{ //This The 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])
$arr ;
$arr[$k]=$tmp;
2. Selection sorting method:
Selection sorting method idea: Select a corresponding element each time, and then place it in the specified position
Copy code
function select_sort($arr) {
//The implementation idea of double loop is completed, the outer layer controls the number of rounds and the current minimum value. The number of comparisons in the inner control
// The known minimum value should be used Compare.
$p = $j;
//If it is found that the position of the minimum value is different from the current assumed position $i, the positions can be interchanged
If($p != $i) {
$tmp = $arr[$p | > return $arr;
}
3. Insertion sort method
Insertion sort method idea: Insert the elements to be sorted into the specified position of the array whose sort number has been assumed.
Copy code
The code is as follows:
function insert_sort($arr) {
//Distinguish which part is sorted
//Which part is unsorted
//Find one of the elements that needs to be sorted
//This element starts from the second element and ends with the last element that needs to be sorted
//It can be marked using a loop
//i loop controls the elements that need to be inserted each time , once the elements that need to be inserted are controlled,
//Indirectly, the array has been divided into 2 parts. The subscript is smaller than the current one (the one on the left), which is the sorted sequence
for($i=1, $ len=count($arr); $i<$len; $i++) {
//Get the current element value that needs to be compared.
$tmp = $arr[$i];
//Inner loop controls comparison and insertion
for($j=$i-1;$j>=0;$j--) {
//$arr[$i];//The element that needs to be inserted; $arr[$j];//The element that needs to be compared
的> // find that the inserted element should be small, and exchange positions
// Exchange the later elements with the previous elements //Set the previous number to the current number that needs to be exchanged Since it is an array that has been sorted, there is no need to compare the previous ones again.
break;
//Return
return $arr;
}
4. Quick sort method
Copy code
The code is as follows:
//First determine whether you need to continue
$length = count($arr);
/ /Traverse all elements except the ruler and put them into two arrays according to their 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 Into the left array
$left_array[] = $arr[$i]; }
}
//Then perform the same sorting on the left and right arrays respectively
//Recursively call this function and record the result
$left_array = quick_sort($left_array);
$right_array = quick_sort($right_array);
//Merge the left ruler and the right
return array_merge($left_array, array($base_num), $right_array);
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In PHP programming, algorithms are an integral part. Mastering common algorithms can not only improve code efficiency, but also help with subsequent program design. The following are common algorithms in PHP programming: Sorting algorithm Sorting algorithm refers to arranging a set of data into an ordered sequence according to certain rules. In PHP programming, commonly used sorting algorithms include bubble sort, insertion sort, selection sort, quick sort, etc. Among them, quick sort is the sorting algorithm with the lowest time complexity and is suitable for processing large-scale data. search algorithm search algorithm

PHP is a very popular programming language that supports various data types and algorithms, of which array sorting and search algorithms are basic and important parts. This article will introduce commonly used array sorting and search algorithms in PHP, as well as their application scenarios and efficiency analysis. 1. Array sorting PHP provides a variety of array sorting methods, including bubble sort, insertion sort, selection sort, quick sort, merge sort, etc. The following is an introduction and sample code for several commonly used algorithms: Bubble Sort (BubbleSort)

With the popularity of the Internet and the continuous expansion of applications, the development of programming languages has become increasingly important. As a very popular programming language, PHP is also constantly developing. In the process of programming with PHP, PHP developers may face the need to represent some knowledge and automatically generate algorithms. So, how to represent knowledge and automatically generate algorithms in PHP? This article will discuss this below. 1. Knowledge representation Knowledge representation is a very important issue in the field of artificial intelligence. Know

PHP algorithm analysis: How to use binary search algorithm to quickly locate elements in an ordered array? Overview: The binary search algorithm is an efficient search algorithm that is suitable for finding specific elements in an ordered array. This article will introduce the principle of binary search algorithm in detail and give PHP code examples. Principle: The binary search algorithm quickly locates the target element by repeatedly reducing the search range by half. The process is as follows: first, narrow the search range to the beginning and end of the array; then, calculate the index of the middle element and compare it with the target element;

In-depth understanding of the core algorithms of PHP and Vue in the brain mapping function Introduction: In the modern Internet era, we often use a variety of applications to help us organize and manage information. Brain mapping is a common and practical way of organizing information, which can graphically display complex thinking processes. In this article, we will focus on the core algorithms of PHP and Vue in the brain mapping function and give code examples. 1. Characteristics of mind maps. Mind maps are a type of brain map that takes a central theme as its core and displays information related to that theme through a tree structure.

PHP algorithm analysis: How to use dynamic programming algorithm to solve the 0-1 knapsack problem? Introduction: Dynamic programming is an algorithmic idea commonly used to solve optimization problems. In program development, the 0-1 knapsack problem is a classic dynamic programming application scenario. This article will introduce how to use PHP to write a dynamic programming algorithm to solve the 0-1 knapsack problem, and provide specific code examples. What is the 0-1 knapsack problem? The 0-1 knapsack problem is a classic combinatorial optimization problem. The problem is set as follows: There is a backpack with a capacity of C. There are n objects

PHP is a widely used development language commonly used for the development of web applications. However, Web applications often need to process large amounts of data, including data processing, storage, query, etc. Therefore, applying algorithms and data structures in PHP is a very critical technology. An algorithm is a general method used in computer programming to solve problems. In programming, we solve problems by designing and implementing algorithms to improve the efficiency, maintainability, and scalability of our programs. Commonly used algorithms include sorting, search, divide and conquer, greedy, etc.

PHP is a scripting language widely used in web development and is getting better and better at building dynamic websites. In Web development, data structures and algorithms are no less important than other programming categories, and their impact on program running efficiency is particularly significant. Especially in scenarios involving large amounts of data storage and processing, or high program performance requirements, data structures and algorithms have become a part that cannot be ignored. This article mainly introduces some commonly used data structures and algorithms in PHP. 1. Data structure array PHP array is a very common
