Sort by sorting, heap sorting, merge sort, high-speed sorting

Source: Internet
Author: User
Tags benchmark

This study note is part of the content from the NetEase cloud class Zhejiang University data Structure video, and Haizi's blog: http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html and ~ Bloomer ~ Blog HTTP://WWW.CNBLOGS.COM/LUCHEN927/ARCHIVE/2012/02/29/2368070.HTML1, choose the basic idea of sorting:In an unordered array of length n. In the first trip to traverse N data, find the smallest value in exchange with the first element, the second traversal of the remaining N-1 data, find the smallest of the values and the second element exchange ... N-1 traverses the remaining 2 data to find out the smallest value exchanged with the first N-1 element. The selection is now complete. Example: Select Sort:first time: traverse these 5 numbers. Find the minimum value of 12. Position at 2, Exchange 1 and 2 position numbers,second time: traverse the remaining 4 numbers. Find the minimum value of 20. Position at 5, Exchange 2 and 5 position numbers, in turn
2, heap sorting is the basic idea of the improvement of the choice of sorting:

1, the initial order to be sorted keyword sequence (r1,r2 .... Rn) is built into the largest heap. This heap is the initial unordered zone;

2. Swap the top element of the heap r[1] with the last element R[n]. The new unordered area is now available (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]<=r[n];

3, because the new heap top r[1] may violate the nature of the heap, it is necessary to the current unordered area (R1,R2,...... RN-1) adjusts to the new maximum heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (R1,R2 ...). Rn-2) and the new ordered area (RN-1,RN).

This process is repeated until the number of elements in the ordered area is n-1. The entire sorting process is complete.

Example: Heap Ordering:16,7,3,20,17,8first time: Build the initial maximum heap: At this point, the Exchange maximum value of 20 and the last position of the heap element gets
Second: The remaining elements except 20 continue to adjust to the maximum heap, and then the elements of the vertex maximum and the second-to-last position are swapped. Get:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "> analogy
Heap ordering of n elements, worst case time complexity O (NLOGN)
3, Merge sort core: The merging of ordered sub-columns two ordered sub-column merging algorithm for example:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">
The basic idea of merging algorithm:the file to be sorted into n a length of 1 ordered sub-file, the sub-file 22 is merged to obtain "n/2" length of 2 of the ordered sub-file, and then this "n/2" an ordered file of sub-file 22 merge. So repeat until you finally get an ordered file of length n. This sort method becomes a two-way merge sort. Example: Merge sort: A.First trip: 72|36 53|31 48|36 Second trip: 72|31 Third trip: three Algorithm: Divide and Conquer:


Merge sort non-recursive algorithm requires extra space O (N)

4, the basic idea of high-speed sequencing:high-speed sorting is to find an element (theoretically can be arbitrarily found) as a datum (pivot), and then partition the array operation, so that the value of the left element of the datum is not less than the datum, the value of the element to the right of the datum is not smaller than the base value, so as the base of the element adjusted to the correct position after Recursive high-speed sequencing. The other n-1 elements are also adjusted to the correct position after sorting. Finally, each element is in the correct position after sorting. Sorting is complete.

How to choose the benchmark?? The idea of the algorithm Median3 is to put the smallest left, middle, and right elements on the far left, and then return to the middle position. When the number is selected as a datum , how do you divide the subset? A pointer to the left and right, respectively, to the middle, when the left hand pointer is incorrect (the left is greater than the datum, the right is less than the correct case). The left and right pointers refer to the digital exchange.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/ Gravity/center "


example:

2 2 4 9 3 6 7 1 5 First use 2 as the benchmark, using the I-j two pointers are scanned from both sides, separating the elements smaller than 2 and the elements larger than 2. First, compare 2 and 5, 5:2, J shift left.

2 2 4 9 3 6 7 1 5 compare 2 and 1. 1 is less than 2, so put 1 in the 2 position

2 1 4 9 3 6 7 1 5 compare 2 and more than 2, so move 4 to the back (original 1 position)

2 1 4 9 3 6 7 4 5 Compare 2 and 7,2 and 6,2 and 3. 2 and 9, all greater than 2. satisfies the condition and therefore does not change

After the first round of high-speed sequencing, the elements change to look like this

[1] 2 [4 9 3 6 7 5]

After that, the 2 left side of the element is quickly ranked. Because there is only one element, the queue ends. The right side is a quick row. Recursively, and finally produces the final result.


Note: When recursive data size is full hour. Then stop the recursion and call the simple sort directly.


Sort by sorting, heap sorting, merge sort, high-speed sorting

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.