Data Structure and Algorithm (Golang Implementation) (18) Sorting Algorithm-Preface

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:

  1. Insert sort: direct insertion sort and Hill sort
  2. Select sorting: direct selection sorting and heap sorting
  3. 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 nsmall, 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 .

Guess you like

Origin www.cnblogs.com/nima/p/12724847.html