Sorting Algorithm
In the development of human beings, we have learned to count. For example, we know how many rabbits Xiaoming hunted today. On the other hand, we also need to judge which one is hunting more today, and we need to compare.
So, the natural need for sorting came out. For example, Xiao Ming hit 5 rabbits, Xiao Wang hit 8 and there were more than a hundred other people from the tribe. We have to talk about merit rewards, whoever plays more, rewards them bigger.
How to sort, how to find the person who hits the most rabbits in the fastest time, this is a very simple question.
After many years of research, many sorting algorithms have appeared, ranging from fast to slow. such as:
- Insert sort: direct insertion sort and Hill sort
- Select sorting: direct selection sorting and heap sorting
- Exchange sort: bubble sort and quick sort
Their complexity is as follows:
Stability concept
Definition: It can guarantee two equal numbers. After sorting, the order of the positions before and after the sequence remains unchanged. (A1 = A2, A1 is in front of A2 before sorting, A1 is still in front of A2 after sorting)
Significance: The essence of stability is to maintain the insertion order of data with the same attributes. Avoid sorting this time.
Bubble sort is arguably the worst sorting algorithm.
We consider bubble sort, direct selection sort, and direct insertion sort as the primary sorting algorithm. The performance of direct insertion sort is the best. Generally speaking, when the sorting array is n
small, direct insertion sort may be better than any sort. The algorithm should be fast. It is recommended to use it only in small-scale sorting.
Hill sorting is an improved version of direct insertion sorting, which is faster than direct selection sorting and direct insertion sorting, and this performance improvement becomes more obvious as the scale increases. Because the algorithm is easy to understand, we can use it when the sorted array is of medium size. At a very large scale, its performance is not so bad, but the following advanced sorting algorithm is recommended for large-scale sorting.
Quick sort, merge sort and heap sort are more advanced sorting algorithms.
At present, it is considered that the best comprehensive advanced sorting algorithm is quick sorting. The average time for quick sorting is the shortest, and it is the sorting algorithm built into most programming libraries.
Heap sorting is also a fast sorting algorithm. By maintaining a binary tree, the root node of the tree is always the largest or smallest to achieve sorting.
Merge sorting uses the divide-and-conquer method just like quick sorting, recursively ordering each subsequence first, and then merging the two ordered sequences into one ordered sequence.
We will explain different sorting algorithms in this chapter.
Series article entry
I am the star Chen, Welcome I have personally written data structures and algorithms (Golang achieve) , starting in the article to read more friendly GitBook .
- Data structure and algorithm (Golang implementation) (1) A simple introduction to Golang-Preface
- Data structures and algorithms (Golang implementation) (2) A simple introduction to Golang-packages, variables and functions
- Data structure and algorithm (Golang implementation) (3) A simple introduction to Golang-flow control statement
- Data structures and algorithms (Golang implementation) (4) A simple introduction to Golang-structures and methods
- Data structure and algorithm (Golang implementation) (5) A simple introduction to Golang-interface
- Data structure and algorithm (Golang implementation) (6) A simple introduction to Golang-concurrency, coroutines and channels
- Data structure and algorithm (Golang implementation) (7) A simple introduction to Golang-standard library
- Data Structure and Algorithm (Golang Implementation) (8.1) Basic Knowledge-Preface
- Data Structure and Algorithm (Golang Implementation) (8.2) Basic Knowledge-Divide and Conquer and Recursion
- Data structure and algorithm (Golang implementation) (9) Basic knowledge-algorithm complexity and progressive symbol
- Data structure and algorithm (Golang implementation) (10) Basic knowledge-the main method of algorithm complexity
- Data Structures and Algorithms (Golang Implementation) (11) Common Data Structures-Preface
- Data structures and algorithms (Golang implementation) (12) Common data structures-linked lists
- Data structures and algorithms (Golang implementation) (13) Common data structures-variable-length arrays
- Data structures and algorithms (Golang implementation) (14) Common data structures-stack and queue
- Data Structures and Algorithms (Golang Implementation) (15) Common Data Structures-List
- Data Structures and Algorithms (Golang Implementation) (16) Common Data Structures-Dictionary
- Data structures and algorithms (Golang implementation) (17) Common data structures-trees
- Data Structure and Algorithm (Golang Implementation) (18) Sorting Algorithm-Preface
- Data structure and algorithm (Golang implementation) (19) Sorting algorithm-bubble sorting
- Data structure and algorithm (Golang implementation) (20) Sorting algorithm-selection sorting
- Data structure and algorithm (Golang implementation) (21) Sorting algorithm-insertion sort
- Data structure and algorithm (Golang implementation) (22) Sorting algorithm-Hill sorting
- Data structure and algorithm (Golang implementation) (23) Sorting algorithm-merge sorting
- Data structure and algorithm (Golang implementation) (24) Sorting algorithm-priority queue and heap sorting
- Data structure and algorithm (Golang implementation) (25) Sorting algorithm-quick sorting
- Data Structure and Algorithm (Golang Implementation) (26) Lookup Algorithm-Hash Table
- Data structure and algorithm (Golang implementation) (27) Search algorithm-binary search tree
- Data structure and algorithm (Golang implementation) (28) Search algorithm-AVL tree
- Data structure and algorithm (Golang implementation) (29) Finding algorithm-2-3 tree and left-leaning red-black tree
- Data structure and algorithm (implemented by Golang) (30) Finding algorithm-2-3-4 tree and ordinary red-black tree