Backend Development
PHP Tutorial
PHP implements bubble sort, selection sort, insertion sort and quick sort quick sort method quick sort c language quick sort algorithm c language
PHP implements bubble sort, selection sort, insertion sort and quick sort quick sort method quick sort c language quick sort algorithm c language
I have read the four basic sorting methods in C language before when I was studying data structure by myself. I have almost forgotten the C language. Recently, when I had time, I re-wrote the four sorting methods in PHP to review the increasingly unfamiliar algorithms. . Paste the code directly.
<?php
/**
* 打印数据,用于调试
* @param var 打印对象
*/
function p($var){
echo "<pre class="brush:php;toolbar:false">";
print_r($var);
echo "";
}
$arr=array(33,11,22,66,55,44,88,99,77);
printf("**原数组**");
p($arr);
/**
*冒泡排序法
* @param $arr 排序数组
*思路:和相邻的数字对比,每次对比如果左边比右边大则交换位置。
*两个节点,一个方向:两次循环次数,冒泡方向(即$j的初值和终止条件)
*num的作用是做了优化,一旦循环没有交换则冒泡已完成
**/
function bubbleSort($arr){
print("**冒泡排序法**");
$len=count($arr);
$temp=0;
$num=1;
for ($i=0; ($i < $len-1)&&($num>0); $i++) {
$num=0;
for ($j=$len-1; $j>$i; $j--) {
if($arr[$j]>$arr[$j-1]){
continue;
}else{
//冒泡交换
$temp=$arr[$j-1];
$arr[$j-1]=$arr[$j];;
$arr[$j]=$temp;
$num++;
}
}
}
return $arr;
}
p(bubbleSort($arr));
/**
*选择排序法
* @param $arr 排序数组
*思路:每一轮循环,找出最小的数字,把其下标保存到最左方数字
*而找出最小数字的方法是:和右方数字比较,若右方比较小,则保存其下标,再将此下标对应的值和
*下一个右方数字比较直到所有右方数字比较一遍,再将此最小值存放在最左方数字
**/
function selectSort($arr){
print("**选择排序法**");
$len=count($arr);
$buff=0;
for ($i=0; $i < $len-1; $i++) {
$temp=$i;
for ($j=$i; $j < $len-1; $j++) {
if($arr[$temp]>$arr[$j+1]){
$temp=$j+1;//若右方数字更小,则保存其下标,用来跟后面的数据比较
}
}
//得到最小的数字下标
if($temp!=$i){
//如果下标不是$i则交换,不然就没必要
$buff=$arr[$temp];
$arr[$temp]=$arr[$i];
$arr[$i]=$buff;
}
}
return $arr;
}
p(selectSort($arr));
/**
*插入排序法
* @param $arr 排序数组
*思路:假设前面的数已经是排好顺序的,现在要把第n个数插到前面的有序数中。即把第二个数据插入到
*第一个数据之中,使其形成一个有序数组,然后再讲第三个数插入到前面两个数组成的有序数组中,形成
*有序数组,如此反复最后完成排序
**/
function insertSort($arr){
print("**插入排序法**");
$len=count($arr);
for ($i=1; $i < $len; $i++) {
$temp=$arr[$i];//将要插入的数据缓存
for ($j=$i-1; $j >=0; $j--) {
if($temp<$arr[$j]){
$arr[$j+1]=$arr[$j];//如果要插入的数据比原数组的最后一个小,则与之交换
$arr[$j]=$temp;
}else break;
}
}
return $arr;
}
p(insertSort($arr));
/**
*快速排序法
* @param $arr 排序数组
*思路:选择一个基准元素,通过一趟扫描,将待排序列分成两部分,再对每一部分递归调用自己方法
*最后返回左边排序好的数组和右边排序好的数组和基准元素合并,就是有序数组了。
**/
function quickSort($arr){
$len=count($arr);
if($len<=1) return $arr;
$baseNum=$arr[0];
$leftArr=$rightArr=array();
//根据基准元素分成两部分
for ($i=1; $i < $len; $i++) {
if ($arr[$i]<=$arr[0]) {
$leftArr[]=$arr[$i];
}else{
$rightArr[]=$arr[$i];
}
}
$leftArr=quickSort($leftArr);//递归调用
$rightArr=quickSort($rightArr);
return array_merge($leftArr,array($baseNum),$rightArr);
}
print("**快速排序法**");
p(quickSort($arr));
?>
Running result:


The above introduces the implementation of bubble sort, selection sort, insertion sort and quick sort in PHP, including quick sort and insertion sort. I hope it will be helpful to friends who are interested in PHP tutorials.
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
1378
52
Java quick sorting tips and precautions
Feb 25, 2024 pm 10:24 PM
Master the key skills and precautions of Java quick sort. Quick sort (QuickSort) is a commonly used sorting algorithm. Its core idea is to divide the sequence to be sorted into two independent parts by selecting a benchmark element, and all the elements in one part are equal. is less than the base element, and all elements of the other part are greater than the base element, then the two parts are recursively sorted, and finally an ordered sequence is obtained. Although quick sort has a time complexity of O(nlogn) in the average case, it degenerates to O(nlogn) in the worst case
How to implement quick sort using Python
Dec 18, 2023 pm 03:37 PM
How to implement quick sorting in Python: 1. Define a function called quick_sort and use the recursive method to implement quick sorting; 2. Check the length of the array. If the length is less than or equal to 1, return the array directly. Otherwise, select the array. The first element is used as the pivot element (pivot), and then the array is divided into two sub-arrays smaller than the pivot element and larger than the pivot element; 3. Connect the two sub-arrays and the pivot element to form a sorted array. .
Quick sort algorithm implemented in Java and its efficiency evaluation
Feb 18, 2024 pm 03:38 PM
Java implementation of quick sort and its performance analysis Quick sort (QuickSort) is a very commonly used and efficient sorting algorithm. It is a divide and conquer (Divide and Conquer) idea. This algorithm divides an array into two sub-arrays, then sorts the two sub-arrays respectively, and finally turns the entire array into an ordered sequence. Quick sort shows excellent performance when processing large-scale data. Quick sort is implemented recursively. The basic idea is as follows: Choose a base
C program for recursive insertion sort
Sep 20, 2023 pm 02:37 PM
Insertion sort is a sorting algorithm that is based on in-place comparison. This algorithm works by placing an element at a position in a sorted subarray, i.e. the subarray before the element is the sorted subarray. Algorithm Step1-Loop from 1 to n-1 and execute-Step2.1-Select the element at position i, array[i]. Step2.2-Insert the element into the sorted sub-array array[0] at its position arr[i]. Let's use an example to understand the algorithm array = [34,7,12,90,51] for i=1, arr[1]=7, put it into the position in the subarray arr[0]-arr[1]. [7,34,12,90,51] For i=2, arr
Quick sort using array functions in PHP
Jun 16, 2023 am 08:54 AM
PHP is a very popular programming language and it is widely used for web development. In PHP, array is a very common data type and a very powerful data structure. Because of this, PHP provides many array functions to help developers handle and manipulate arrays. This includes the quick sort function, which helps us sort arrays quickly. Quick sort is a common sorting algorithm. Its basic idea is to divide an array into two sub-arrays, one smaller than the other, through comparison and exchange, and then recursively
How to implement quick sort algorithm using java
Sep 19, 2023 am 11:28 AM
How to implement the quick sort algorithm in Java Quick sort (QuickSort) is a commonly used and efficient sorting algorithm. Its basic idea is to use the divide and conquer strategy (Divide and Conquer). By selecting one element at a time as the benchmark value, the array to be sorted is divided into two parts, one part is smaller than the benchmark value, and the other part is larger than the benchmark value, and then the two parts are processed separately. Recursive sorting, and finally achieve sorting of the entire array. Below we will introduce in detail how to use Java language to achieve rapid sorting
What is quick sort in C language?
Sep 26, 2023 am 11:00 AM
Quicksort is a frequently used sorting algorithm due to its popularity and popularity relative to other sorting algorithms.
How to quickly sort functions in java
Jan 18, 2024 pm 05:26 PM
Quick sort method: 1. Create a Java sample file; 2. Implement the quick sort algorithm through the quickSort method; 3. Select an element in the array as the pivot (pivot), and divide the array into two sub-arrays, one containing the pivot The element with the smaller element is the smaller element, and the other contains the element that is larger than the pivot element, and then the quick sort algorithm is recursively applied to the two sub-arrays; 4. Sort the array in the main method and output the result.


