Home Backend Development PHP Tutorial Simple sorting algorithm java sorting algorithm summary of sorting algorithm heap sort algorithm

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

Jul 29, 2016 am 08:52 AM
Sorting Algorithm Simple

$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教程有兴趣的朋友有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The easiest way to query the hard drive serial number The easiest way to query the hard drive serial number Feb 26, 2024 pm 02:24 PM

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

Complex experimental design issues in Kuaishou's two-sided market Complex experimental design issues in Kuaishou's two-sided market Apr 15, 2023 pm 07:40 PM

1. Background of the problem 1. Introduction to the two-sided market experiment The two-sided market, that is, a platform, includes two participants, producers and consumers, and both parties promote each other. For example, Kuaishou has a video producer and a video consumer, and the two identities may overlap to a certain extent. Bilateral experiment is an experimental method that combines groups on the producer and consumer sides. Bilateral experiments have the following advantages: (1) The impact of the new strategy on two aspects can be detected simultaneously, such as changes in product DAU and the number of people uploading works. Bilateral platforms often have cross-side network effects. The more readers there are, the more active the authors will be, and the more active the authors will be, the more readers will follow. (2) Effect overflow and transfer can be detected. (3) Help us better understand the mechanism of action. The AB experiment itself cannot tell us the relationship between cause and effect, only

How to write a simple student performance report generator using Java? How to write a simple student performance report generator using Java? Nov 03, 2023 pm 02:57 PM

How to write a simple student performance report generator using Java? Student Performance Report Generator is a tool that helps teachers or educators quickly generate student performance reports. This article will introduce how to use Java to write a simple student performance report generator. First, we need to define the student object and student grade object. The student object contains basic information such as the student's name and student number, while the student score object contains information such as the student's subject scores and average grade. The following is the definition of a simple student object: public

Quick Start: Use Go language functions to implement a simple library management system Quick Start: Use Go language functions to implement a simple library management system Jul 30, 2023 am 09:18 AM

Quick Start: Implementing a Simple Library Management System Using Go Language Functions Introduction: With the continuous development of the field of computer science, the needs of software applications are becoming more and more diverse. As a common management tool, the library management system has also become one of the necessary systems for many libraries, schools and enterprises. In this article, we will use Go language functions to implement a simple library management system. Through this example, readers can learn the basic usage of functions in Go language and how to build a practical program. 1. Design ideas: Let’s first

How to write a simple online reservation system through PHP How to write a simple online reservation system through PHP Sep 26, 2023 pm 09:55 PM

How to write a simple online reservation system through PHP. With the popularity of the Internet and users' pursuit of convenience, online reservation systems are becoming more and more popular. Whether it is a restaurant, hospital, beauty salon or other service industry, a simple online reservation system can improve efficiency and provide users with a better service experience. This article will introduce how to use PHP to write a simple online reservation system and provide specific code examples. Create database and tables First, we need to create a database to store reservation information. In MyS

How to write a simple music recommendation system in C++? How to write a simple music recommendation system in C++? Nov 03, 2023 pm 06:45 PM

How to write a simple music recommendation system in C++? Introduction: Music recommendation system is a research hotspot in modern information technology. It can recommend songs to users based on their music preferences and behavioral habits. This article will introduce how to use C++ to write a simple music recommendation system. 1. Collect user data First, we need to collect user music preference data. Users' preferences for different types of music can be obtained through online surveys, questionnaires, etc. Save data in a text file or database

How to use PHP to develop simple file management functions How to use PHP to develop simple file management functions Sep 20, 2023 pm 01:09 PM

Introduction to how to use PHP to develop simple file management functions: File management functions are an essential part of many web applications. It allows users to upload, download, delete and display files, providing users with a convenient way to manage files. This article will introduce how to use PHP to develop a simple file management function and provide specific code examples. 1. Create a project First, we need to create a basic PHP project. Create the following file in the project directory: index.php: main page, used to display the upload table

How to write a simple minesweeper game in C++? How to write a simple minesweeper game in C++? Nov 02, 2023 am 11:24 AM

How to write a simple minesweeper game in C++? Minesweeper is a classic puzzle game that requires players to reveal all the blocks according to the known layout of the minefield without stepping on the mines. In this article, we will introduce how to write a simple minesweeper game using C++. First, we need to define a two-dimensional array to represent the map of the Minesweeper game. Each element in the array can be a structure used to store the status of the block, such as whether it is revealed, whether there are mines, etc. In addition, we also need to define

See all articles