After reading the data structure, I ended the sorting of the last chapter. The idea of reading data in a week is probably realized. Of course it is not for speed. This time, we mainly focus on understanding various data structures and clear concepts, learn about the classic algorithms and algorithm concept. In the future, if you want to use specific structures and algorithms, go to the introduction to algorithms. The following are the study notes for today.
Ranking)
1. sorting can be viewed as an operation on a linear table. Sorting by multiple keywords can be converted into sorting by a single keyword. The sorting is stable and unstable. The Sorting Algorithm is mainly influenced by three aspects: time performance, auxiliary space, and algorithm complexity.
2. By complexity: simple algorithms (Bubble sorting, simple selection sorting, direct insertion sorting), improved algorithms (Hill sorting, heap sorting, Merge Sorting, and quick sorting)
3. Bubble Sorting
Basic Idea: it is a kind of exchange sorting that compares the keywords of the same adjacent records in two pairs. If the reverse order is used, it is exchanged until there is no reverse order record.
Basic version: when looking for a record with a small I, compare the elements in the grid with the I + 1, and put the small I bit; continue to compare I bit with I + 2 until I is in the nth bit comparison and exchange, and get the record with a smaller I value; perform the next round of searching for records smaller than I + 1.
Authentic Bubble Sorting: when looking for records smaller than I, you must compare and exchange n-I records at the same time (N total records ), small elements are moved to the position I After comparison.
Bubble optimization: If there is no switching in a round of floating (set a flag for switching), it is ordered. After the stop, the bubble ends the program.
4. Simple selection and sorting
Principle: Through the n-I comparison, the minimum record from the n-i-1, It is exchanged with the I-th record;
Difference from bubble: Bubble is compared with one exchange at a time, while the simple choice method is to perform one exchange after multiple comparisons in a round.
5. Insert sorting directly
Principle: Insert a record to an ordered table that has already been sorted to obtain a new ordered table with an increase of 1 in the number of records. It is similar to the poker process.
6. Hill sorting
Basic Order: The smallest is the first, and the largest is the last.
Principle: Skip segmentation-unstable. The results of directly inserting records from a specific increment into a subsequence are still basically ordered. Decrease the interval from n/2 to 1 according to the regular order (the last increment must be 1)
7. Heap sorting
Heap: A Complete Binary Tree of this nature-the value of each node is greater than or equal to the value of its left and right child nodes-the big top heap (or <= small top heap ).
Sorting Background: The comparison results are not saved during each round of comparison. As a result, the comparison results are repeated in the next several rounds. The improvement is that when the minimum record is selected in each round, adjust other records based on the comparison results of this round.
Sorting principle: Construct the sequence to be sorted into a heapadjust function. At this time, the maximum value of the sequence is the root node, after it is removed, A New Complete Binary Tree is generated by filling the end element of the tree, and the heapadjust function is used to re-adjust it to a large top heap. Repeat the above process until the last node with children.
Heapadjust, the constructor of the Big Top heap, finds the current node and Its left and right subnodes, swaps the subnodes, and finds the swap of its subnodes.
8. Merge Sorting
Principle: assume that the initial sequence contains N records and is considered as an ordered sub-sequence with a length of 1. The two are sorted and merged (Merge function ), obtain the minimum integer ordered sequence of no less than n/2, and then merge them to obtain an ordered sequence of N length. recursion;
Merge function: merge two ordered sequences Sr [I .. m], Sr [M + 1 .. n]; I, j corresponds to the record, who is small, who is stored in tr [K], who is ++, the other is unchanged, while K ++; enter the next round, I, continue to compare the elements corresponding to j .., until I increases to m or J to n.
Improve recursive Merge Sorting by memory usage: Use iteration.
9. Quick sorting
Principle: sort the sequence by one click and divide it into two parts. The records on the left are smaller than the pivot, and the records on the right are larger than the pivot. -- Implement the partiton function; then, separate the left and right parts to implement recursion.
Partiton function (returns the position of the pivot cursor after the left and right are arranged): in each round, find the right end of the sequence J-the first one is smaller than the swap, so that the small one is on the left, find the first one in the sequence starting from I ++ on the left side of the sequence that is larger than that in the sequence, so that the greater one is on the right side of the sequence until I and j are equal.
Optimization-selection of pivot orientation in partiton Function Algorithm Implementation, fixed selection, random selection, three-digit fetch, and nine-digit fetch;
Optimization of unnecessary swapping: The partiton function sorts the left and right sides of each round. It is compared multiple times, but only once.
Optimize the sorting scheme for small arrays: when high-low is smaller than a constant, directly sort it by direct sorting.
Optimized Recursion TO iteration, saving space.
10. select the most appropriate sorting method based on the time performance (Best, worst, average), Space performance, stability, and number of applicable sequences.