Sorting Algorithm summary, Sorting Algorithm
Sorting algorithms are often used to evaluate a person's basic abilities. Therefore, the study of the Sorting Algorithm summary, combing, textbooks refer to the Netease cloud in the class of Zhejiang University opened in the data structure of the course mentioned in the sorting content, attached to the link: http://mooc.study.163.com/learn/ZJU-1000033001? Tid = 1000044001 #/learn/content? Type = detail & id = 1000112012 & cid = 100015100. Thank you. If any error occurs, please correct it. -- 2015.6.30
Several Concepts used:
Stability: the relative position of any two equal data before and after sorting does not change.
Thought: it repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the smaller elements will slowly "float" to the top of the series through the exchange.
Pseudocode:
The explanation is clear. Note that the flag is used to distinguish between them. If there is no exchange in this round, it indicates that the order has been arranged and it can be completed.
Time Complexity: Order T = O (N), reverse order T = O (N2)
Stability: Stability
Small Test 1: Perform Bubble Sorting for 7 numbers. In the worst case, the number of comparisons is (the answer is 21 ).
Thought: By constructing an ordered sequence, for unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it.
Pseudocode:
It is easy to understand.
Time Complexity: Order T = O (N), reverse order T = O (N2)
Stability: Stability
Test 2: How many times does it take to complete element exchange for the given initial sequence {34, 8, 64, 32, 21}, Bubble sorting, and insert sorting? Answer: nine times each
Thought: it is a more efficient improved version of insert sorting. It sets an interval of h. Each time multiple I and I + h pairs are exchanged by insert sorting, reduce h and then repeat the SWAp until h = 1, and then perform the last swap to complete the sorting.
Pseudocode: The increment is N/2. You need to carefully draw an example to understand it. This is easy to remember.
Time Complexity: not fixed, worst O (N2)
Stability: due to multiple insert sorting, we know that one insert sorting is stable and does not change the relative sequence of the same elements. However, in different insert sorting processes, the same elements may move in their respective insert sorting, and the final stability will be disrupted, so shell sorting is unstable.
Thought: First, find the smallest (large) element in the unordered sequence, store it to the starting position of the sorting sequence, and then continue to find the smallest (large) element from the remaining unordered elements, put it at the end of the sorted sequence. And so on until all elements are sorted.
Pseudocode:
The position of finding the smallest element is achieved by comparing the size, that is to say to compare the N-1 times
Time Complexity: not fixed, worst O (N2)
Stability: unstable
Idea: accumulation is a structure that is approximately a Complete Binary Tree and meets the accumulation nature. That is, the key value or index of a subnode is always smaller than (or greater than) its parent node.
Pseudocode:
Each time you find the current largest node through PercDown, the first step of BuildHeap is to create a maximum heap, and the second step is to swap the largest node with the last node (because the maximum value is at the end of the sorting result ), next, adjust the following to the largest heap, switch the largest number to the end each time, and then discard the sorting until the sorting is completed.
Time Complexity: the average is O (nlogn), and the space complexity is O (1)
Stability: unstable
Thought: merge operations (merge), also known as merge algorithms, refers to operations that combine two sorted sequences into one. The Merge Sorting Algorithm depends on the merge operation.
Pseudocode:
Core:
Recursive mode:
Non-recursion:
The merge operation process is as follows:
1) apply for a space, so that the size is the sum of two sorted sequences, which is used to store the merged Sequence
2) set two pointers. The initial position is the start position of the two sorted sequences respectively.
3) Compare the elements pointed to by the two pointers, select a relatively small element and put it in the merging space, and move the pointer to the next position.
4) Repeat Step 3 until a pointer reaches the end of the sequence.
5) Copy all the remaining elements of another sequence directly to the end of the merging sequence.
Time Complexity: both mean and worst are O (nlogn), and space complexity: O (N)
Stability: Stability
Idea: the Divide and conquer policy is used for fast sorting to Divide a sequence (list) into two subsequences (sub-lists ).
Pick out an element from a series, called a benchmark, and re-sort the series. All elements smaller than the benchmark value are placed before the benchmark, all elements are placed behind the benchmark (the same number can reach any side ). After the partition ends, the benchmark is in the middle of the series. This is called a partition operation.
Recursively (recursive) sorts the subseries smaller than the reference value element and the subseries larger than the reference value element.
The bottom of recursion is that the number of columns is zero or one, that is, they are always sorted. This algorithm always ends after recursion, because in each iteration, it will at least place an element at its final position.
Pseudocode:
The first section is a method for finding a token, that is, by comparing the beginning, end, and center values, getting a median value and putting it at the n-1 position, then in the algorithm, compare and replace it from the left and right, recursively solve the problem. When the data volume is smaller than cutoff, insert sorting is used.
Time Complexity: average O (nlogn), worst O (n2), space complexity: best O (logn), worst O (n)
Stability: unstable
Idea: a non-Comparative integer sorting algorithm, in which the principle is to cut the integer into different digits by the number of digits, and then compare them by each digit. Since integers can also express strings (such as names or dates) and floating point numbers in specific formats, the base sorting is not only applicable to integers. The base sorting method can be LSD (Least significant digital) or MSD (Most significant digital). The LSD sorting method starts from the rightmost of the key value, while MSD is the opposite, start from the leftmost of the key value.
Pseudocode:
Time Complexity: O (k * n), n is the number of sorting elements, k is the number of digits, space complexity: O (k * n)
Stability: Stability
Each sort algorithm has its own applicability. Under certain conditions, quick sorting is the most suitable.
Link: http://www.ming-yue.cn/sort-conclusion/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.