search
HomeCommon ProblemWhat are the basic sorting methods?

Basic sorting methods include: 1. Selection sort, which is divided into "simple selection sort" and "heap sort"; 2. Insertion sort, which is divided into "simple insertion sort" and "Hill sort"; 3. Exchange sorting is divided into "bubble sort" and "quick sort"; 4. Merge sort; 5. Radix sort.

What are the basic sorting methods?

Basic sorting method

Sort

No A sorting algorithm is optimal under any circumstances, and the optimal algorithm must be selected according to the actual situation to solve the problem

Algorithm stability: In a set of records to be sorted, if there are any two equal Records R and S, and in the records to be sorted, R is before S. If R is still before S after sorting, that is, their front and back positions do not change before and after sorting, then the sorting algorithm is called stable

  • Selection Sort

Simple Selection Sort

Simple Selection Sort is an intuitive The sorting algorithm selects the smallest element in the unsorted sequence and exchanges it with the first element of the sequence, and then selects the smallest element in the remaining unsorted sequence and exchanges it with the second element of the sequence, and so on. Finally, a sorted sequence from small to large is formed

Time complexity: O(N2)

Heap sort

Generate the unordered sequence into a maximum heap, and put the top of the heap Swap the elements with the last element, and generate the maximum heap with the remaining elements. Exchange the elements in sequence and generate the maximum heap

Time complexity: O(NlogN) Space complexity: O(1)

  • Insertion sort

Simple insertion sort

Divide a set of sequences to be sorted into sorted There are two parts, ordered and unsorted. In the initial state, the sorted sequence only contains the first element, and the elements in the unsorted sequence are N-1 elements except the first one; thereafter, the elements in the unsorted sequence are added one by one. Insert into sorted sequence. In this way, after N-1 insertions, the number of elements in the unsorted sequence is 0, then the sorting is completed

Time complexity: O(N2) Stable sorting

Hill sorting

Divide a set of elements to be sorted into several sequences at certain intervals, and perform insertion sorting respectively. The "interval" set at the beginning is larger, and the interval is gradually reduced in each round of sorting, until the "interval" is 1, that is, the last step is to perform simple insertion sorting

Time complexity: and increment The selection of sequences is related to non-stable sorting

  • Exchange sorting

Bubble sorting

For elements When sorting N sequences to be sorted, a total of N-1 cycles are performed. In the k-th loop, the elements from the 1st to the N-kth are compared from front to back, and the two adjacent elements are compared each time. If the former element is greater than the latter element, the two exchange positions, otherwise they remain Position unchanged

Time complexity: O(N2)

Quick sort

Divides the unsorted elements into two subsequences based on a "pivot" as the basis, The records of one subsequence are all greater than the pivot, while the records of the other subsequence are smaller than the pivot, and then the two subsequences are sorted recursively using a similar method

Time complexity: O(Nlog2N)

  • Merge sort

Treat a sequence of size N as N subsequences of length 1. Next Merge adjacent subsequences in pairs to form N/2 (1) ordered subsequences with a length of 2 (or 1); then continue to merge adjacent subsequences in pairs. If you keep looping, until If there is one sequence left with length N, then this sequence is the result of sorting the original sequence

Time complexity: O(Nlog2N) Space complexity: O(N)

  • Radix sort

#Bucket sort

If it is known that the value range of N keywords is from 0 to M- 1, and M is much smaller than N, the bucket sorting algorithm creates a "bucket" for each value of the keyword, that is, M buckets are created, and when scanning N keywords, each keyword is placed into the corresponding bucket, and then collect them in the order of the buckets to be naturally ordered

radix sort

Radix sort is a generalization of bucket sort. The records to be sorted it considers include More than one keyword

The above is the detailed content of What are the basic sorting methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Video Face Swap

Video Face Swap

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

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools