Summary of common sorting algorithms

Source: Internet
Author: User

1,

 

2. What is the significance of studying the stability of sorting algorithms?

First of all, we should know the stability of the sorting algorithm. In general, it is to ensure that the first two equal data in the sorting sequence are in the same order as those after sorting.

Simply put, if a I = A J, AI is before the Aj position, and the AI is still before the Aj position after sorting.

The following describes the benefits of stability:

(1) If the Sorting Algorithm is stable, sort from one key and then from another. The result of the first key can be used by the second key.

In this case, the data elements with the same low position will not change even if they are in the high position. For more information, see Basic sorting.

(2) When learning the sorting principle, the elements to be sorted in the program may be simple types. In fact, the actual application may be a complicated type (custom type) array sorting,

The sort key value is only one attribute of this element. For a simple type, the numeric value is all its meaning, and it cannot be seen any difference even if it is exchanged.

However, for complex types, swapping may cause elements that should not be exchanged. For example, a "student" array, to sort by age, the "student" object contains not only "Age", but also many other attributes.

If the original array uses the student ID as the primary key for data sorting from small to large. Stable sorting ensures that two students of the same age are not exchanged during comparison.

That means that even though the "Age" is sorted, the student ID order is still from small to large.

3. Stability Analysis of various sorting algorithms

Now let's analyze the stability of common sorting algorithms, and each gives a simple reason.

(1) Bubble Sorting

The Bubble sorting method is to call a small element forward (or call a large element backward ). Note that the two adjacent elements are compared, and whether the exchange is required also occurs between the two elements.

Therefore, if the two elements are equal, I think you will not be bored with switching them again.

If two equal elements are not adjacent, the two elements will not be exchanged even if they are adjacent through the two pairs in the front, so the order of the same elements after sorting has not changed.

Therefore, Bubble Sorting is a stable sorting algorithm.

(2) Select sorting

Selecting sorting means selecting the smallest element in the elements to be sorted for each position. For example, if the minimum value is selected for the first position, a small value is selected for the second position in the remaining element,

And so on until the n-1 element, the N element does not need to be selected, because only one of its largest elements is left.

When selecting a lock element, if the current lock element is larger than the next one, and the smaller one appears after an element equal to the current lock element, after the switch, the location order is obviously changed.

Haha! For example, if the sequence is 5, 8, 5, 2, 9, we know that the first choice of 1st elements 5 will exchange with 2, then the relative sequence of the two 5 in the original sequence is damaged.

Therefore, selecting sorting is not a stable sorting algorithm.

(3) Insert sorting

Insert sorting inserts an element at a time based on an ordered small sequence. Of course, at the beginning, this ordered small sequence had only one element, that is, the first element (it is ordered by default ).

The comparison starts from the end of the ordered sequence, that is, comparing the elements to be inserted with the sorted sequence. If it is larger than it, it is directly inserted behind it.

Otherwise, search until you find the inserted position. If you encounter an element that is equal to the inserted element, place the element to be inserted after the element that is equal.

Therefore, the order of equal elements is not changed, and the order from the original unordered sequence is still sorted, so insertion sorting is stable.

(4) Fast sorting

There are two directions for quick sorting. The I subscript on the left is always directed to the right (when Condition A [I] <= A [center_index]), where center_index is the array subscript of the central element, it is generally set to an array of 0th elements.

The J subscript on the right always goes to the left (when a [J]> A [center_index ).

If I and j cannot move, I <= J, exchange a [I] And a [J], repeat the above process until I> J. Exchange a [J] And a [center_index] to complete a quick sorting.

When the central element is exchanged with a [J], it is very likely to disrupt the stability of the preceding elements. For example, the sequence is 5 3 3 4 3 8 9 10 11.

Now the exchange of central element 5 and 3 (5th elements, subscript starting from 1) will disrupt the stability of element 3.

Therefore, quick sorting is an unstable sorting algorithm, which occurs when the central element is exchanged with a [J.

(5) Merge and sort

Merge Sorting refers to recursively dividing a sequence into short sequences and Recursion

 

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.