Summary of common sorting algorithms based on comparison

Source: Internet
Author: User

A comparison-based sorting algorithm should be the most intuitive way to fit people. In the technical books of various algorithms, it has been proved that the time-optimal complexity of the comparison-based sorting algorithm is O (NLOGN).  Here are a few common comparison-based sorting algorithms: 1. Select sort: This should be the most intuitive sorting method. When ordering n elements, the first traversal, finds the smallest element, swaps it with the first element, second traversal, finds the minor element and swaps it with the second element, until the last element is left.  2. Bubble sort: This should be the first sort algorithm we've learned. The basic idea is that by comparing the adjacent two elements in turn, such as the latter value is smaller than the previous value, then the two values are exchanged, the small value is exchanged to the front, and the large value is exchanged to the back. After such a traversal, the maximum value is put to the last. And the small value is exchanged before n-1. Then traverse the front n-1,n-2 again until the last 2 elements. Over the course of the process, the small value is gradually switched to the front as it progresses through the process, much like bubbles gradually emerge from the bottom. So is called the bubbling algorithm.  3. Insert sort: The idea of this algorithm is very intuitive. According to the explanation in the introduction to the algorithm, this algorithm can refer to our usual poker situation. When grabbing a card, compare the hand in order and insert it in the appropriate position. This ensures that all the cards in hand are still in order. When the number of sorted values is large,is guaranteed to be orderly, the insertion position can be determined by means of a binary lookup when determining where the new value is to be inserted. 4. Hill sort: In the bubbling algorithm, the small value can only be moved forward with a step speed of 1. The hill sort is optimized for step size and begins to group with a larger m step, with each group being inserted to sort, thus achieving a move of step m. Then gradually reduce the step m until 1. So the fundamental idea is to move the element farther than possible instead of moving one bit. 5. Merge sort: This thought is used to solve the problem of a common thought, divide-and-conquer, that is, divide and conquer the thought. The n elements are 2 points each time, into two N/2 element groups, up to 1 elements-1 elements, is naturally ranked. Then, 22 merges the group of elements and eventually merges into an element group. Merging algorithms, because of the need to merge, it is necessary to need an additional n space to achieve the merge. 6. Quick sort: The same is the idea of divide and conquer, the original data is divided into 2 groups. But with the merge algorithm directly divides the original data into two parts, the fast row selects a median, the new two subgroups, all the elements of a subgroup are less than the median, and the other subgroup all elements are greater than or equal to the median until the number of elements is 1. When the number of elements is 1 o'clock, the queue is actually sorted. This is also different from the merge sort, which does not require additional action after the subgroup is complete. Obviously, the efficiency of the Clippers depends on the median choice. If the median can divide the data into two equal numbers of subgroups, then the efficiency is the highest. No additional storage space is required for the in-place to be sorted.7. Heap sequencing: The idea is to treat the primitive elements as a balanced binary tree. The parent node is then required to adjust the balanced binary tree to a rule that is larger than the child node. Because it is a balanced binary tree, the data is perfectly divided into equal portions. This is the maximum value for the root node. At this point, the heap sort completes the selection of the maximum value. To sort, the root node is exchanged with the last child node. At this point, the tree's rules are broken and the rules need to be verify from the root node.

Summary of common sorting algorithms based on comparison

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.