[Review of data structure] the sorting algorithm of eight inner-ranking algorithm

Source: Internet
Author: User

= = = = = = = = = = = N More information, mainly reference http://blog.csdn.net/hguisu/article/details/7776068 and http://blog.csdn.net/onedreamer/article/ A summary of details/6745006 =============== stability and time complexity


When n is large, you should use the time complexity of O (nlog2n) Sorting method: Quick Sort, heap sort or merge sort order.
Fast sorting is considered as the best method at present based on the comparison, when the keyword to be sorted is randomly distributed, the average time of fast ordering is shortest;

(1) Order of squares (O (n2))
All kinds of simple sorting: direct insertion, direct selection and bubble sort;
(2) Order of linear Logarithmic order (O (NLOG2N))
Quick sort, heap sort and merge sort;
(3) O (n1+§)) Sort, § is a constant between 0 and 1.
Hill sort
(4) Linear Order (O (n)) ordering
Cardinality ordering, in addition to bucket, box sorting.
Description
When the original table is ordered or basically ordered, the direct insertion sort and bubble sort will greatly reduce the number of comparisons and the Times of moving records, and the time complexity can be reduced to O (n);
But the quick order is opposite, when the original table is basically orderly, degenerate is the bubble sort, the time complexity increases to O (N2);
The order of the original table has little effect on the time complexity of simple selection, heap sort, merge sort and cardinal order. Second, the sorting algorithm thought summary

The following is a summary of their ordering ideas in the order shown above. 1. Insert Directly

Inserts a record into a sorted ordered table to get a new ordered table with a record number of 1. If you encounter an element that is equal to the insertion element, the insertion element places the elements that you want to insert behind the equality element. Therefore, the order of the equal elements is unchanged, the order from the original unordered sequence is the order after the sequence, so the insertion sort is stable.
The other insertion sort has a binary insertion sort, 2-way insertion sort.
The second point is easy to understand: Add a two point lookup in the insertion method. Not to repeat.
2-Road: Its purpose is to reduce the number of data movement, so, in addition to open up auxiliary space. First, a temporary array of length n is created, and the 1th element of the array to be sorted is placed in the No. 0 position of the temporary array as initialization. The value is used as a reference for each sort, and is inserted before the reference value is equal to, and is inserted before the reference value. Also defines two cursors, first and final, pointing to the location of the current minimum and maximum values of the temporary array respectively. As shown in the following illustration:
2.shell sorting

The relative direct ordering has a great improvement. Hill sort is also called narrowing the incremental sort. First, the whole sorted record sequence is divided into several sub sequences for direct insertion sort, and then the whole records are inserted into order directly after the records are "basically ordered" in the entire sequence.
Action method:
Select an increment sequence T1,T2,...,TK, in which Ti decrements, tk=1;
By the increment sequence number k, the sequence is ordered by K-trip;
On each trip, according to the corresponding increment ti, the backlog sequence is segmented into a number of sub sequences of length m, and the child tables are directly inserted and sorted respectively. Only the increment factor is 1 o'clock, the entire sequence is treated as a table, and the length of the table is the length of the entire sequence.
We simply deal with the increment sequence: Increment sequence d = {N/2, N/4, N/8 .... 1} n is the number of numbers to sort
That is, the group of records that will be sorted is divided into several groups of n/2,n by an increment D (the number of numbers to be sorted). The subscript of a record in each group D. Inserts a direct sort of all elements in each group, then groups it with a smaller increment (D/2), and then inserts the sort directly in each group. Continue to shrink incrementally until 1, and finally use the direct insert sort to complete the sort.
Example:
3. Direct Selection

In the set of numbers to be sorted, select the smallest (or largest) number and the number of the 1th position to exchange; then find the smallest (or largest) number in the 2nd position in the remaining number, and so on, until the n-1 element (the penultimate number) and the nth element (the last number) are compared.
An improvement to the direct selection sort is:
One cycle to find the largest and smallest number, respectively, on the front and back. This can reduce the number of cycles in half. 4. Heap Sorting

The heap corresponds to a complete binary tree
Building a Heap method: the process of building a heap on an initial sequence is a process of filtering over and over again.
1 The complete binary tree of n nodes, the last node is the subtree of the first node.
2 filter starts with a subtree that is the root of the first node, and the subtree becomes a heap. (How to: Start with the current node, with the smallest of its left and right children, if it is larger than the youngest child, swap positions, and continue to compare downward until childless.) )
3) Then the tree of the root of each node is filtered to make it a heap until the root node.
The initial process of the graph build heap: Unordered sequence: (49,38,65,97,76,13,27,49)

Patch code to help understand

 void Heapadjust (int h[],int s, int length) {int tmp = H[s]; int child = 2*s+1; The location of the left child's node. (Child+1 is the location of the right child node for the current adjustment node) while (Baby < length) {if (child+1 <length && H[child]>h[chil  
        D+1]) {++child;  
            } if (H[s]

* * After outputting the heap top element, rebuild the stack of adjustments to the remaining n-1 elements.
How to adjust the small top heap: * *
1 with m elements of the heap, output heap top elements, left m-1 elements. The heap bottom element is fed to the top of the heap (the last element is exchanged with the top of the heap) and the heap is corrupted because the root node does not satisfy the nature of the heap.
2 The root node is exchanged with the smaller elements in the left and right subtree.
3 If the Zuozi Exchange: If the Shozi is destroyed, that is, Zuozi root node does not meet the nature of the heap, then repeat the method (2).
4 If it is exchanged with the right subtree, if the right subtree is destroyed, the root node of the right subtree does not satisfy the nature of the heap. The Repeat method (2).
5 continue to carry on the above exchange operation to the subtree that does not satisfy the heap nature, until the leaf node, the heap is built.
The adjustment process of the root node to the leaf node is screened. As shown in figure:

The final ranking algorithm also utilizes the above Heapadjust method:

/** 
 * Heap sorting algorithm 
 *  
/void heapsort (int h[],int length)  
{  
    //initial heap  
    buildingheap (H, length);  
    Adjusts the sequence from the last element for  
    (int i = length-1 i > 0; i)  
    {  
        //swap heap top element h[0] and the last element in the heap  
        int temp = h[i]; H[i] = h[0]; H[0] = temp;  
        The heap is adjusted  
        heapadjust (h,0,i) each time the top element of the heap is exchanged and the last element in the heap;  
   
5. Bubble sort

In the set of numbers to be sorted, the total number in the range that is not yet well ordered is compared and adjusted from top to bottom two consecutive numbers, allowing the larger numbers to sink and the smaller to go up. That is, whenever two adjacent numbers are compared and found to be in reverse order, they are interchanged.
Improved:
Add a variable to record whether there is an exchange for this traversal. If not, stop the loop. 6. Quick Sort

Remember to be recursive.
1 Select a datum element, usually select the first element or the last element,
2 by a trip to sort the records to be sorted to split into two separate parts, some of which record element values are smaller than the value of the base element. The other part of the record has an element value larger than the Datum value.
3 The correct position of the datum element at this time after its orderly
4 and then the two parts of the record in the same way continue to sort, until the entire sequence ordered.
Example:
(a) a process of sequencing:

(b) The whole process of sequencing

An improvement in a fast row:
A fast ordering of recursive calls of a subsequence that is greater than k is used, so that the original sequence is basically ordered, and then the entire basic ordered sequence is sorted by an insert sort algorithm. Practice shows that the improved algorithm has a lower time complexity, and the improved algorithm has the best performance when the K value is about 8. 7. Merge sort

The merge (merge) Sort method combines two (or more) ordered tables into a new ordered table, which divides the sequence into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.
Example:

Merge method:
Set R[I...N] consists of two ordered child tables R[I...M] and R[M+1...N], two child table lengths are N-i + 1, n-m respectively.
1.j=m+1;k=i;i=i; The starting subscript of the two child tables and the starting subscript of the auxiliary array
2. If I>m or j>n, turn ⑷//one of the child tables has been merged, compare Select End
3.//Select R[i] and r[j] smaller in auxiliary array RF
If R[i] 8. Cardinal Order

First look down bucket sort: three, concrete code realization

Look at the ideas above to write yourself.
To be supplemented later. 1. Insert Directly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.