69 comparison sort of "algorithm"

Source: Internet
Author: User
Tags sorts

Review Comparison sort

The children's shoes, who believe they have read the previous 5 posts, have found that "in the final result of a sort, the order of each element depends on the comparison between them". As such, this sort of sorting algorithm is collectively referred to as the "comparison sort".

A comparison order reads a list element by a single, abstract comparison operation (such as "less than equals"), which determines which of each two elements should appear first in the final sorted list.

Disclaimer: The following is a list of comparison sorts by using the very perfect illustrations found in Wikipedia.

Insert Sort

In this series of "algorithm" 1 we introduced the basic algorithm, its comparison process is as follows:

The following is an animation that sorts an array of 30 elements by inserting a sort:

Select Sort

The comparison process for selecting a sort is as follows:

The animation works as follows:

Merge Sort

The previous write to merge sort, its comparison process is as follows:

The animation for merge sort is as follows:

Heap Sort

In this series of "algorithm" 4 we introduced the Fast Platoon, the process of building the heap is as follows:

The heap sort animations are as follows:

Quick Sort

In this series of "algorithm" 5 we introduced the fast row, its comparison process is as follows:

The animations for quick sorting are as follows:

Some other sort of comparison

The following sorts are also comparison sorts, but not previously mentioned in the series.

Intro Sort

The algorithm is a hybrid sorting algorithm that starts with fast sorting and switches to heap ordering when the recursion depth exceeds the level based on the number of elements being sorted. It contains the good parts of both algorithms, and its actual performance is equivalent to a quick sort on a typical data set and a heap ordering in the worst case scenario. Because it uses two comparison sorts, it is also a sort of comparison.

Bubble Sort

Everyone should have listened to the bubble sort (also known as the sinking sort), it is a very basic sorting algorithm. Repeatedly comparing the two adjacent elements and swapping them appropriately, if there are no elements in the list that need to be interchanged, indicates that the list is already sorted. (See the list to think about six months ago to learn the scheme, welcome everyone to see, I opened 2 columns to introduce them)

The above description already embodies the process of comparison, so bubble sort is also a sort of comparison, the smaller element is called "bubble (Bubble)", it will "float" to the top of the list.

Although this algorithm is very simple, but you should also hear, it is really very slow.

The process of bubbling the sort is as follows:

Animated demo of bubbling Sort:

The best and worst-case run times are: Θ(n) 、 Θ ( n 2 ) 。

odd-even sort

Odd and even sort and bubble sort have many similar characteristics, it by comparing all the odd index in the list of adjacent elements, if there is a pair is the wrong sort (that is, the former is larger than the latter), then exchange them, and then repeatedly repeat this step until the entire list is ordered.

For this reason, the best and worst-case run times are the same as the bubbling sort: Θ(n) 、 Θ ( n 2 ) 。

The odd-even sort is demonstrated as follows:

The following is an example of parity ordering in C + +:

Template<classT>voidOddevensort (T a[],intN) { for(inti =0; I < n; i++) {if(I &1)//' I ' is odd{ for(intj =2; J < N; J + =2)             {if(A[j] < a[j-1]) Swap (a[j-1], a[j]); }          }Else{ for(intj =1; J < N; J + =2)              {if(A[j] < a[j-1]) Swap (a[j-1], a[j]); }           }    }}
bidirectional bubble Sort

The two-way bubble sort is also known as cocktail ordering, cocktail bartender sequencing, shaker sequencing, ripple sorting, shuffle sorting, shuttle bus sequencing, and more. (no more gorgeous name can make up for its inefficiency)

The difference between a bubble sort is that it iterates through the list in two directions, though it does not improve the asymptotic performance and does not have much advantage over the insertion sort.

Its best and worst-case run times are the same as the bubbling sort: Θ(n) 、 Θ ( < Span style= "Position:absolute; Clip:rect (2.029em 1000.003em 2.776em-0.477em); Top: -2.611em; Left:0.003em; " >n 2 ) 。

Can we do a lot of sorting operations?

This depends on the computational model, which is simply what you are allowed to do.

69 comparison sort of "algorithm"

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.