[Data structure] complexity of common sorting algorithms

Source: Internet
Author: User

Related Concepts

1. stable sorting (stable sort) and non-stable sorting

Stable sorting means that all equal numbers can still maintain their relative order before sorting after some sort algorithm operation. The opposite is non-stable sorting.

2. internal sorting and external sorting)

In the sorting process, all the numbers to be sorted are in the memory and their storage order is adjusted in the memory, called inner sorting. During the sorting process, only some of the numbers are transferred to the memory, the sort method for storing memory adjustment numbers in external storage is called external sorting.

 

Sorting Algorithm

[Bubble Sorting] (Bubble Sort)

The Bubble sorting method is the simplest sorting method. The basic idea of this method is to regard the elements to be sorted as vertical "Bubbles", and the smaller elements are relatively light, so as to move up and down. In the Bubble sorting algorithm, we need to process the "bubble" sequence several times. The so-called one-time processing is to check the sequence from the bottom up, and always pay attention to whether the order of two adjacent elements is correct. If the order of two adjacent elements is incorrect, that is, the "light" elements are located below, and their positions are exchanged. Obviously, after processing it again, the "lightest" element floated to the highest position; after processing it twice, the "minor" element floated to the lower position. During the second processing, you do not have to check because the element at the highest position is already the lightest element. In general, the I-th processing does not have to check the elements above the I-th high position, because they are sorted correctly after the processing of the previous I-1 times.

Bubble Sorting is stable. The algorithm time complexity is O (n2 ).

 

 

Selection Sort)

The basic idea of sorting is to process the sequence of sorted records over n-1 times. The I-times processing is to swap the smallest of [I. n] With position I. In this way, after I times, the position of the previous I record is correct.

The sorting is unstable. The algorithm complexity is O (n2 ).

 

 

Insert sorting (Insertion Sort)

The basic idea of insertion sorting is that after I-1 processing, L [1 .. I-1] has arranged the order. The I-th processing only inserts L into the proper position of L [1 .. I-1], making L [1. I] A sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L and L [I-1], if L [I-1] ≤ L, then L [1 .. i] the order has been sorted, the I-times processing is over; otherwise the position of switching L and L [I-1] continues to compare L [I-1] and L [I-2], until a position j (1 ≤ j ≤ i-1) is found so that L [j] ≤ L [j + 1.

Direct insertion and sorting are stable. The algorithm time complexity is O (n2)

 

 

Heap Sort)

Heap sorting is A kind of tree-based sorting. During the sorting process, A [n] is considered as A Complete Binary Tree sequential storage structure, use the inner relationship between parent and child nodes in A Complete Binary Tree to select the smallest element.

Heap sorting is unstable. Algorithm time complexity O (nlog2n ).

 

 

[Merge Sort] (Merge Sort)

The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence.

Merge Sorting is stable. The time complexity is O (nlog2n) in both the best and worst cases ).

 

Quick Sort)

Quick sorting is an essential improvement of Bubble sorting. The basic idea is that after scanning, the length of the sorting sequence can be greatly reduced. In Bubble sorting, A scan can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may be reduced by 1. By performing a quick sorting scan, you can make sure that the numbers on the left of a certain number (based on it) are smaller than that on it, and the numbers on the right are larger than that on it. Then, we use the same method to process the numbers on both sides of it until there is only one element on the left and right of the benchmark.

 

Fast sorting is unstable. Ideally, the algorithm time complexity O (nlog2n) and the worst O (n ^ 2 ).

 

Comparison of sorting methods

 

 

The time complexity of the Bubble Sorting Algorithm is O (n ^ 2)

The time complexity of the Sorting Algorithm is O (n ^ 2)

The time complexity of inserting sorting algorithms is O (n ^ 2)

Fast sorting is unstable. Ideally, the algorithm time complexity O (nlog2n) and the worst O (n ^ 2 ).

The time complexity of the heap sorting algorithm is O (nlogn)

The time complexity of the Merge Sorting Algorithm is O (nlogn)

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.