If the internal sorting methods are classified according to different principles in the sorting process, they can be roughly divided into five categories: insertion sorting, exchange sorting, selection sorting, Merge Sorting, and allocation sorting; the sorting process can be divided into three categories:
1. A simple sorting method with the time complexity of O (n2)
2. the time complexity of the improved sorting method is O (nlog2n).
3. The time Degree of the base sorting method is O (D * n ).
there are only two basic operations in the sorting process: Comparison record and moving record.
common algorithms are classified into five types:
· insertionsort ), there are direct insertion sorting, binary insertion sorting, and shell sorting.
· exchangesort, which includes Bubble sorting and quick sorting (partition exchange sorting ).
· selectionsort: You can directly select sorting and heap sorting.
· mergesort
· distributionsort, also known as base sorting.
In order to visually explain how each sorting algorithm works, let's take a look at how to sort cards in disorder on the table using these methods. Cards should be sorted by colors (plum blossom, square, red peach and black heart in sequence), and also by points (from 2 to ).
sort by insertion : starts from the top of a pile of cards, each time you get a card, place the cards in the correct position according to the sorting principle. The cards on the table are sorted.
switching and sorting :
(1) Put two cards in your hands first. If the cards on the left are placed behind the cards on the right (the color or points on the left are larger than those on the right), place the cards on them.
(2) win a card and compare the two cards on the rightmost side. If necessary, switch the positions of the two cards.
(3) Repeat Step (2) until you get all the cards.
(4) if you no longer need to switch the positions of any two cards in your hands, the cards are sorted. Otherwise, put the cards in your hands on the table and repeat them (1) go to step (4) Until the cards in your hands are sorted.
when you select sort , find the smallest card on the card list and put it in your hand; repeat this operation until you put all the cards in your hands.
merge and sort : divide the cards on the table into 52 heaps, each of which is a card. Because each heap of cards is ordered (Remember, there is only one card in each heap at this time), if two adjacent heap cards are merged into one heap and each heap card is sorted, you can get 26 stacked cards in order, each of which has two cards. Repeat this merge operation to get 13 heap cards (4 cards in each heap) and 7 heap cards (6 heap cards are 8 cards, there are 4 more cards in the stack), and finally we will get 52 cards.
distributed sorting (also called Radix sort, that is, base sorting) is performed as follows: first, divide the cards into 13 stacks by points, then fold the 13 stacks in order of points, and then divide the cards into 4 stacks by colors, then the four cards are stacked in order of colors, and the cards are arranged in order.
2. Why is it N2, nlog2n, or D * n?
The sorting process is essentially an insert process.
A simple Sorting Algorithm inserts the nth number into other (n-1) numbers. N (n-1) times are required for N numbers, so the time complexity cannot exceed N2.
The basic idea of the improved sorting algorithm is "decomposition": select an appropriate keyword and divide a sorting task into two small sorting tasks. Ideally, the keyword can always break down the original sorting task into two groups of sorting tasks with the same length. Therefore, in the next round of sorting, you only need to compare in this group (the length is 1/2 of the previous round ). This process is equivalent to a binary sorting tree. The root segment contains N data elements to generate a binary sorting tree, which is equivalent to inserting a new node in the binary sorting tree n times. Therefore, the time complexity does not exceed O (nlog2n ).
References: answers to frequently asked questions about C programming, such as Pauls S. R. Chisholm, translated by Dr. Zhang fangni lubo to Tsinghua University Press 1996