Comparison of multiple sorting algorithms

Source: Internet
Author: User

1. Bubble sorting and selection sorting

Why are Bubble sorting and selection sorted together? Because I found the two of them look a little like.

Bubble Sorting constantly switches the largest element to the rightmost of the array.

The selection of sorting is to change the smallest element to the leftmost end.

Seeing this, do you think bubble is no different from choice? Changing the maximum value to the minimum becomes a new algorithm? So I have one?

In fact, it does not matter whether it is the biggest or the smallest, even if the bubble becomes the smallest element to change to the leftmost end of the array, it is also called the bubble sort.

Description of Bubble Sorting:It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need to exchange, that is, the sequence has been sorted.

Select the sorting description:Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged.

To facilitate the description of the differences between the two, we replace bubble and sort with the largest element to the rightmost end.

Their biggest difference is that

Bubble Sorting constantly compares two adjacent elements and constantly exchanges large elements until the end of the array.

When sorting is selected, only comparison is not exchanged until the end of the array is exchanged.

Here, you can think that selecting sorting is the optimization of Bubble sorting, because selecting sorting reduces the number of exchanges most of the time, but achieves the same effect.

Isn't the exchange of Bubble Sorting all useless? Think about it, of course not!

Let's take a closer look at what Bubble Sorting means,"The work of visiting series is repeated until there is no need to exchange ",This is very important. If we add a variable to record the number of exchanges, when the number of traversal exchanges is zero, it means that the array has been sorted, then the sorting can be completed!

For example, "1, 2, 3, 4, 5, 6, 7, 8, 9", for such an array, the bubble sort only needs to be traversed once, so for the basic ordered array, bubble can achieve O (n) complexity.

However, sorting cannot be done. It takes almost the same time for an ordered array and a random array to be sorted (a little less time, because an ordered array does not need to be exchanged and only needs to be compared ).

Here, we find that the initial input state of the array is not used for sorting, but the Bubble Sorting is used. This is the advantage of bubble. Here, you may wonder how bubble uses the initial state? Of course, it is in the comparison of Adjacent Elements again and again.

Finally, the selection of sorting will swap non-adjacent elements, for example, directly switching a [0] And a [8] locations, resulting in unstable sorting. Bubble only exchanges adjacent elements, so bubble is stable. Of course we will not be stupid as a [0] = 1, when a [1] = 1, switch their positions, right.

Later, we will analyze insertion sorting and Hill sorting. The Causal Analysis of the Stability of these two sorting is also related to the exchange of non-adjacent elements.

Come here first, and write again tomorrow ..

Related Keywords:

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.