Algorithm of sorting algorithm _ algorithm

Source: Internet
Author: User
Tags sorts
(A book "Programmer's Practical Algorithm" (Andrew Binstock/john Rex), the Internet has a Chinese electronic version, but recommended to buy a genuine, this is a classic book. )

1. Overview

The sorting algorithm is the most basic algorithm in the computer technology, and many complex algorithms use the sort. Although all sorts of sorting algorithms have been encapsulated into library functions for programmers to use, it is important to understand the idea and principle of sorting algorithm for writing high quality software.

This paper introduces the common sorting algorithm, and sums up the algorithm idea, complexity and usage scene.

2. Several concepts

(1) Sort Stability: If two numbers are the same, the sort result for them is the same as their relative order. For example a={1,2,1,2,1} here is a = {1,1,1,2,2} Stability is sorted after the first 1 is sorted before the first 1, the second 1 is the second 1 before the sort, and the third 1 is the third 1 before the sort. Likewise 2 is the same. Instability is that their order is inconsistent with the starting order.

(2) in-situ ordering: the sort that does not apply for extra space, that is, to compare and exchange in the original sorted data. For example, quick sort, heap sort etc. are sorted in place, merge sort, count sort, etc. not in situ sort.

In general, there are two kinds of design ideas for the sorting algorithm, one based on the comparison and the other not based on the comparison. The book "Introduction to Algorithms" shows the proof: "The optimal time complexity of the algorithm based on the comparison is O (n lg N)". For the algorithm based on comparison, there are three kinds of design ideas, namely: Insert sort, exchange sort and select sort. Non-comparison based sorting algorithms have a time complexity of O (LG N), so the complexity is so low because they generally have special requirements for sorting data. If the counting sort requires that the data range not be too large, the cardinality ordering data can be decomposed into multiple attributes.

3. Ranking algorithm based on comparison

As described in the previous section, the ranking algorithm based on comparison has three design ideas, namely inserting, exchanging and selecting. For the insertion sort, there are mainly direct insertion sort, hill sort, and for exchange sort, there are mainly bubble sort, fast sort, and for selection sort, there are simple selection sort, heap sort, and other sort: merge sort.

3.1 Insert Sort

(1) Direct insertion Sort

Features: Stable sort, in-situ sequencing, time complexity O (n*n)

Thought: Divide all the data to be sorted into two sequences, one is ordered sequence s, the other is sorted sequence U, at first, S is empty, U is a series of all data, and then the data in U is inserted into ordered sequence s until u becomes empty.

Applicable scenario: When the data has been basically ordered, the insertion sort can significantly reduce the data exchange and the number of data movement, thus improving the sorting efficiency.

(2) Hill sort

Features: Unstable sort, in-situ sequencing, time complexity O (N^LAMDA) (1 < Lamda < 2), LAMDA and each step size selection.

Thought: Incrementally narrowing the sort. By dividing the sequence incrementally into groups of approximate elements, the direct insertion sort method is used to sort each group, then the increment is reduced to 1, and the sort is completed using the direct insert sort.

Scenario: This algorithm is not commonly used because incremental initial values are not easy to select.

3.2 Exchange Sort

(1) Bubble sort

Features: Stable sort, in-situ sequencing, time complexity O (n*n)

Thought: The whole sequence is divided into unordered and ordered two subsequence, and the sorting is done continuously by exchanging large elements to the first unordered subsequence.

Scenario: Similar to direct insertion sort

(2) Quick sort

Features: Unstable sort, in-situ sequencing, time complexity O (n*lg N)

Thought: Constantly looking for a pivot point for a sequence, then move the data less than and greater than the pivot point to both sides of the pivot point, and then continue the operation on both sides of the series until the whole sequence is sorted.

Applicable scenarios: Widely used, almost all languages provide a fast-scheduling API

3.3 Select Sort

(1) Simple selection sort

Features: Unstable sort (for example, 3 3 23 numbers are sorted, first 3 will be exchanged with 2), in-situ sequencing, time complexity O (n*n)

Thought: The sequence is divided into unordered and ordered two subsequence, looking for the minimum (large) value and the first element exchange of the disordered sequence in the unordered sequence, the ordered region expands one, the loop goes on, and finally completes the whole order.

Applicable scenario: Less exchange

(2) Heap sorting

Features: Unstable sort, in-situ sequencing, time complexity O (n*lg N)

Thought: Small top heap or large pile

Applicable scenario: Not as fast as the broad

3.4 Other Sorts

(1) Merge sort

Features: Stable sort, not in-situ sequencing, time complexity O (n*n)

Thought: First, the whole sequence (a total of n elements) as n ordered subsequence, and then merge the adjacent two subsequence, so that it goes on until it becomes a whole ordered sequence.

Applicable scenario: External sort

4. Non-comparison based sorting algorithm

There are three kinds of non comparison based sorting algorithms: Cardinal order, bucket sort and counting sort. These algorithms are for special data, as well as the need for uniform data distribution, data deviation is not too large. The idea is to use memory for time, so the whole is right and wrong in the sort.

4.1 Cardinal Order

Features: Stable sort, not in-situ sequencing, time complexity O (N)

Thought: Each data as a D attribute composition, in turn, according to the D attribute to the data sorting (each round can be sorted by counting), the complexity of O (d*n)

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

4.2 Bucket Sort

Features: Stable sort, not in-situ sequencing, time complexity O (N)

Thought: The data by size to several barrels (such as linked list) inside, each barrel using a simple sorting algorithm to sort.

Applicable scenario: 0

4.3 Counting sort

Features: Stable sort, not in-situ sequencing, time complexity O (N)

Idea: Technology for each number of occurrences (using the hash method count, the simplest hash is an array.) ), and then output each data from large to small or to large.

Usage scenarios: More extensive than cardinality sorting and bucket sorting.

5. Summary

For comparison based sorting algorithms, most of the simple sort (direct insertion sort, select sort and bubble sort) are stable sorted, except for selection, most advanced sorting (except for simple sort) is an unstable sort, excluding the merge sort, but the merge sort requires additional storage space. For non comparison based sorting algorithms, they have special requirements for data laws, and they use the idea of memory exchanging time. With so many sorting algorithms, it is often necessary to choose the most suitable sorting algorithm according to the practical application.

6. Reference materials

(1) Bowen "Sorting algorithm Summary":

Http://www.cppblog.com/shongbee2/archive/2009/04/25/81058.html

(2) Bowen: "Eight sorting algorithm":

Http://blog.csdn.net/yexinghai/archive/2009/10/10/4649923.aspx

———————————————————————————————-

For more information on data structures and algorithms, please see: Data structure and algorithm summary

———————————————————————————————-

Original articles, reproduced please specify: Reprinted from Dong's Blog

This article link address: http://dongxicheng.org/structure/sort/

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.