Simple sorting algorithm java sorting algorithm summary of sorting algorithm heap sort algorithm

WBOY
Release: 2016-07-29 08:52:55
Original
1396 people have browsed it

$arr = array(
12,
45,
89,
3,
24,
55,
223,
76,
22,
1 1,
28,
112,
20,
434,
23,
65,
65,
765,
6,
8,
23,
5,
33,
553,
4 5,
423,
64,
77,
84,
23
);
/**
* Bubble sort algorithm, time complexity n2/2 times
* The basic concept of bubble sort is: compare two adjacent numbers in sequence, put the decimal in front and the large number in the back . That is, in the first pass: first compare the first and second numbers, put the decimals in front and the large numbers in the back.
* Then compare the second number and the third number, put the decimal in front and the big number in the back, and continue like this until comparing the last two numbers, put the decimal in front and the big number in the back. At this point, the first trip is over, and the largest number is placed at the end.
* In the second pass: still start the comparison from the first pair of numbers (because it may be due to the exchange of the second number and the third number that the first number is no longer smaller than the second number), put the decimal first, After the large number is put, the comparison is continued until the second to last number
* (the first to last position is already the largest). At the end of the second pass, a new maximum number is obtained at the second to last position (in fact, the entire is the second largest number in the sequence). Continue like this, repeat the above process,
* until the sorting is finally completed.
*/
function maopao_sort($arr)
{
$count = count($arr);
$tmp;
$m = 0; // Used to calculate how many times to execute
for ($i = 0; $i < $count - 1; $i ++) {
for ($j = 0; $j < $count - 1 - $i; $j ++) {
if ($arr[$j] > $arr[$j + 1]) {
                                                                                                                                                    }
$count = count ($arr);
$tmp;
$m = 0; // Used to calculate how many times it has been executed
for ($i = 0; $i < $count - 1; $i ++) {
$p = $i;
for ($j = $i + 1; $j < $count; $j ++) {
if ($arr[$p] > $arr[$j]) {
$p = $j;
                                                                                                                                                    ] = $arr[$i];
              $arr[$i] = $tmp;
                                                                                                                 print_r( = count($arr);
$tmp;
$m = 0; // Used to calculate how many times it has been executed
for ($i = 1; $i < $count; $i ++) {
$tmp = $arr[$i];
for ($j = $i - 1; $j >= 0; $j --) {
                                                                                                                 $ j +1] = $ arr [$ j];
$ arr [$ j] = $ tmp;
} else {
break;
}
$ m ++;
Echo $m;
}
/**
* Quick sorting algorithm, time complexity n2/2 times
* The basic idea of ​​this method is:
* 1. First take a number from the sequence as the base number.
* 2. During the partitioning process, all numbers larger than this number are placed on its right side, and all numbers smaller than or equal to this number are placed on its left side.
* 3. Repeat the second step for the left and right intervals until there is only one number in each interval.
 */
function quick_sort($arr)
{
    $count = count($arr);
    if ($count <= 1) {
        return $arr;
    }
    $tmp = $arr[0];
    $left_array = array();
    $right_array = array();
    
    for ($i = 1; $i < $count; $i ++) {
        if ($arr[$i] <= $tmp) {
            $left_array[] = $arr[$i];
        } else {
            $right_array[] = $arr[$i];
        }
        $m ++;
    }
    
    $left_array = quick_sort($left_array);
    $right_array = quick_sort($right_array);
    return array_merge($left_array, array(
        $tmp
    ), $right_array);
}
// print_r(quick_sort($arr));
// // print_r(quickSort($arr));
function quickpaixu($arr)
{
    $count = count($arr);
    if ($count <= 1) {
        return $arr;
    }
    $key = $arr[0]; // 取一个值,稍后用来比较;
    $left_arr = array();
    $right_arr = array();
    for ($i = 1; $i < $count; $i ++) { // 比$key大的放在右边,小的放在左边;
        if ($arr[$i] <= $key) {
            $left_arr[] = $arr[$i];
        } else {
            $right_arr[] = $arr[$i];
        }
    }
    $left_arr = quickpaixu($left_arr); // 进行递归;
    $right_arr = quickpaixu($right_arr);
    return array_merge($left_arr, array(
        $key
    ), $right_arr); // 将左中右的值合并成一个数组;
} // 以下是测试
  // print_r(quickpaixu($arr));
?>

以上就介绍了简单排序算法,包括了排序算法,简单方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template