To sum up the Sorting Algorithm

Source: Internet
Author: User
1. Overview

Sorting algorithms are the most basic algorithms in computer technology. Many complex algorithms use sorting. Although various sorting algorithms have been encapsulated into library functions for programmers to use, understanding the ideas and principles of sorting algorithms is very important for writing high-quality software.

This article introduces common sorting algorithms and summarizes the algorithm ideas, complexity, and application scenarios.

2. Several Concepts

(1)Stable sorting: If the two numbers are the same, the sorting results for them will not change their relative order. For example, a = {, 1} after sorting here is a = {, 2} stable is the first 1 after sorting is the first 1 Before sorting, the second 1 is the first 1, and the third 1 is the first 1. Likewise, the same is true for 2. Instability means that their order is inconsistent with the starting order.

(2)In-situ sorting: Sorting by comparing and exchanging the original sorting data without applying for additional space. For example, fast sorting and heap sorting are all in-situ sorting, Merge Sorting, and count sorting.

In general, there are two design ideas for sorting algorithms: Comparison and comparison. The book introduction to algorithms provides a proof that "the optimal time complexity of a comparison-based algorithm is O (n LG n )". There are three designs for comparison-based algorithms: insert sorting, exchange sorting, and select sorting. The time complexity of non-comparison-based sorting algorithms is O (lg n), which is so low because they generally have special requirements for sorting data. For example, Count sorting requires that the data range is not too large, and base sorting requires that data can be divided into multiple attributes.

3. Comparison-based Sorting Algorithm

As described in the previous section, comparison-based sorting algorithms have three design ideas: insert, exchange, and selection. For insertion sorting, there are mainly direct insertion sorting and Hill sorting; for exchange sorting, there are Bubble sorting and quick sorting; for selection sorting, there are mainly simple selection sorting and heap sorting; other sorting: Merge Sorting.

3.1 insert and sort (extra space required)

(1) Insert directly to sort

Features: stable sorting, in-situ sorting, time complexity O (N * n)

Thought: divide all the data to be sorted into two sequences: one is the ordered sequence s and the other is the sequence u to be sorted. Initially, S is empty and U is a series composed of all the data, then, insert the data in U into the ordered sequence s until u becomes null.

Use Cases: when data is already in basic order, insertion sorting can significantly reduce the number of data exchanges and data moves, thus improving the sorting efficiency.

(2) Hill sorting

Features: unstable sorting, in-situ sorting, time complexity O (n ^ lamda) (1 <lamda <2), and lamda are related to each step selection.

Idea: sort by incremental reduction. First, the sequence is divided into several groups with the approximate number of elements by increment. Then, the sequence is sorted by direct insert sort, and the increment is reduced to 1. Finally, the sequence is sorted by direct insert.

Applicable scenarios: Because incremental initial values are not easy to select, this algorithm is not commonly used.

3.2 swap sorting (no extra space required)

(1) Bubble Sorting

Features: stable sorting, in-situ sorting, time complexity O (N * N)

Thought: the whole sequence is divided into two sub-sequences: unordered and ordered, and the sorting is continuously completed by exchanging large elements to the first of the unordered sub-sequences.

Applicable scenarios: similar to direct insertion sorting

(2) Fast sorting (taking a central point)

Features: unstable sorting, in-situ sorting, time complexity O (N * lg N)

Thought: keep searching for the pivot points of a sequence, move the data smaller than or greater than the pivot point to both sides of the pivot point, and then continue this operation in the series on both sides, until all sequences are sorted.

Application Scenario: It is widely used and provides quick APIs in almost all languages.

3.3 select sorting (no extra space required)

(1) Simple selection and sorting

Features:Unstable sorting(For example, sorting 3, 3, 2, 3, and 3 will exchange with 2), in-situ sorting, time complexity O (N * N)

Idea: divide the sequence into two subsequences: unordered and ordered, and find the minimum (large) value in the unordered sequence and the first element exchange of the unordered sequence. The ordered area expands one and loops down, finally, all sorting is completed.

The number of records in the sorted sequence is n. I: 1, 2 ,..., N-1, from all n-I + 1 records (Ri, Ri + 1 ,..., Rn) to find the record with the smallest sorting code and exchange it with the I record. The sorting of the record sequence is completed after the n-1 round is executed.

Applicable scenarios: less exchange

(2) Heap sorting

Features: unstable sorting, in-situ sorting, time complexity O (N * lg N)

Idea: small top heap or large top heap

Applicable scenarios: not as wide as fast sorting

3.4 other sorting

(1) Merge and sort

Features: stable sorting, non-in-situ sorting, time complexity O (N * n)

Thought: first, think of the whole sequence (n elements in total) as N ordered subsequences, and then merge the adjacent two subsequences in sequence, until it becomes an overall ordered sequence.

(2) External sorting

Applicable scenarios: External sorting (massive data, splitting m Files, sorting each file internally, retrieving 1st data of each file, initializing the heap, and placing the first data in the output buffer zone, take the next data in the file "previous data already in the buffer zone", put it in the heap, perform heap sorting, and then put the data on the top of the heap into the buffer zone for recursion, until all data in the file is cleaned and the heap is empty <the maximum number of heap data is m>)


4. Non-comparison-based sorting algorithms

There are three non-comparison-based sorting algorithms: Base sorting, bucket sorting, and count sorting. These algorithms are applicable to special data. They do not require even data distribution, and the data deviation is not too large. The idea is to change the memory time, so they are all non-in-situ sorting.

4.1 base sorting

Features: stable sorting, non-in-situ sorting, time complexity O (N)

Thought: Think of each data as composed of D attributes, Sort data by D attributes in sequence (count sorting can be used for each round), complexity is O (D * n)

Applicable scenario: data is composed of several key words or attributes.

4.2 bucket sorting

Features: stable sorting, non-in-situ sorting, time complexity O (N)

Idea: divides data into several buckets (such as linked lists) by size, and sorts each bucket by simple sorting algorithm.

Applicable scenarios: 0

4.3 count sorting

Features: stable sorting, non-in-situ sorting, time complexity O (N)

Idea: count the number of occurrences of each data (use the hash method to count, the simplest hash is an array !), Then, output each data from large to small or from small to large.

Application Scenario: it is more extensive than base sorting and bucket sorting.

5. Summary

For comparison-based sorting algorithms, most of the simple sorting (direct insertion sorting, selection sorting and Bubble Sorting) are stable sorting, except sorting; most of the advanced sorting (except simple sorting) they are all non-stable sorting, except the Merge Sorting, but the Merge Sorting requires additional storage space. For non-comparison-based sorting algorithms, they all have special requirements on data rules and adopt the idea of memory-to-time. When there are so many sorting algorithms, you often need to select the most suitable Sorting Algorithm Based on the actual application.

6. References

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.