PHP兑现常见排序算法
PHP实现常见排序算法
//插入排序(一维数组)
function insert_sort($arr){
?$count = count($arr);
?for($i=1; $i?$tmp = $arr[$i];
?$j = $i - 1;
?while($arr[$j] > $tmp){
?$arr[$j+1] = $arr[$j];
?$arr[$j] = $tmp;
?$j--;
?}
?}
?return $arr;
}
//选择排序(一维数组)
function select_sort($arr){
?$count = count($arr);
?for($i=0; $i?$k = $i;
?for($j=$i+1; $j?if ($arr[$k] > $arr[$j])
?$k = $j;
?if ($k != $i){
?$tmp = $arr[$i];
?$arr[$i] = $arr[$k];
?$arr[$k] = $tmp;
?}
?}
?}
?return $arr;
}
//冒泡排序(一维数组)
function bubble_sort($array){
?$count = count($array);
?if ($count ?
?for($i=0; $i?for($j=$count-1; $j>$i; $j--){
?if ($array[$j] ?$tmp = $array[$j];
?$array[$j] = $array[$j-1];
?$array[$j-1] = $tmp;
?}
?}
?}
?return $array;
}
//快速排序(一维数组)
function quick_sort($array){
?if (count($array)
?$key = $array[0];
?$left_arr = array();
?$right_arr = array();
?for ($i=1; $i
?else
?$right_arr[] = $array[$i];
?}
?$left_arr = quick_sort($left_arr);
?$right_arr = quick_sort($right_arr);
?
?return array_merge($left_arr, array($key), $right_arr);
}
?>

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

Uniapp is a cross-platform development framework. Its powerful cross-end capabilities allow developers to develop various applications quickly and easily. It is also very simple to implement drag-and-drop sorting and drag-and-drop operations in Uniapp, and it can support drag-and-drop operations of a variety of components and elements. This article will introduce how to use Uniapp to implement drag-and-drop sorting and drag-and-drop operations, and provide specific code examples. The drag-and-drop sorting function is very common in many applications. For example, it can be used to implement drag-and-drop sorting of lists, drag-and-drop sorting of icons, etc. Below we list

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

The Count function is used to count the number of numbers in a specified range. It ignores text, logical values, and null values, but counts empty cells. The Count function only counts the number of cells that contain actual numbers. The CountA function is used to count the number of non-empty cells in a specified range. It not only counts cells containing actual numbers, but also counts the number of non-empty cells containing text, logical values, and formulas.

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb
