Time complexity of various sorting algorithms

Source: Internet
Author: User
Tags sorts

Choosing sort, Quick sort, Hill sort, Heap sort is not a stable sorting algorithm, bubble sort, Insert sort, merge sort, and Cardinal sort are stable sorting algorithms.

The sort algorithm's instability means that there are two numbers equal before Sorting. But after the sort is over, they are two likely to change the Order.

For example:
In a queue to sort, A and B are equal, and a is in front of b, and A is followed by B after sorting. at this point, we say that the algorithm is Unstable.
(as long as there is this possibility, we say that the algorithm is not stable.)
Note: the instability of the algorithm is not related to the language used.

First of all, the stability of the sorting algorithm should be known, popularly speaking is to ensure that the first 2 equal number of the sequence before and after the order and the ordering of their two before and after the position of the same order. In a simple formalization, if ai = Aj, The AI is originally in position, the AI is still in front of the Aj Position.

Stability of the common sorting algorithm

(1) Bubble Sort

The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two Elements. so, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 Exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.

(2) Select Sort

The choice of sorting is to choose the smallest current element for each location, such as selecting the smallest one for the first position, selecting the second small for the second element in the remaining element, and so on, until the n-1 element, the nth element is not selected, because it is left with one of its largest elements. then, in a trip, if the current element is smaller than an element, and the small element appears behind an element that is equal to the current element, then the Post-swap stability is Destroyed. Compare the awkward, for example, sequence 5 8 5 2 9, We know that the first time to select the 1th element 5 and 2 exchange, then the original sequence of 2 5 of the relative sequence is destroyed, so the selection of sorting is not a stable sorting algorithm.

(3) Insert Sort
An insert sort is an element that is inserted one at a time, based on an already ordered small sequence. Of course, There are only 1 elements at the beginning of this ordered sequence, which is the first Element. The comparison starts at the end of the ordered sequence, that is, the element that you want to insert and the one that is already in order, is inserted directly behind it if it is larger than it is, or until it is found where it is inserted. If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal Element. so, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.

(4) Quick Sort
The quick sort has two directions, the left I subscript goes right, when a[i] <= a[center_index], where Center_index is the array subscript of the central element, generally takes the No. 0 element of an array. and the right J subscript goes left, when a[j] > a[center_index]. If I and J are not moving, I <= j, Exchange a[i] and a[j], repeat the process above until I>J. Exchange a[j] and a[center_index], to complete a quick sort of a trip. When the central element and a[j] are switched, it is very possible to disrupt the stability of the previous element, such as the sequence is 5 3 3 4 3 8 9 10 11, now the central element 5 and 3 Exchange will be the stability of element 3 is disturbed, so fast sorting is an unstable sorting algorithm, instability occurs in the central element and a[j] Exchange of Time.

(5) Merge Sort
The merge sort is to divide the sequence recursively into the short sequence, the recursive exit is the short sequence only 1 elements (think directly ordered) or 2 sequences (1 comparison and exchange), then merges each ordered segment sequence into an orderly long sequence, merges continuously until the original sequence is all Ordered. It can be found that when 1 or 2 elements, 1 elements are not exchanged, and 2 elements are equal in size and no one is intentionally exchanged, which does not destabilize the Stability. so, in the process of merging short ordered sequences, is stability compromised? no, we can guarantee that if the two current elements are equal, we keep the elements in the preceding sequence in front of the result sequence, which guarantees Stability. therefore, The merge sort is also a stable sorting algorithm.

(6) Base Order
The cardinality sort is sorted by low, then collected, sorted by high order, then collected, and so on, until the highest bit. Sometimes some properties are prioritized, sorted by low priority, then sorted by high priority, and the final order is high priority high, high priority is the same low-priority high. Base sorting is based on sorting separately and is collected separately, so it is a stable sorting algorithm.

(7) Hill Sort (shell)
Hill sort is the insertion of elements according to different steps, when the first element is very disordered, the step is the largest, so the number of elements inserted in the order is very small, fast; when the elements are basically ordered, the step size is very low, and the insertion sort is very efficient for ordered Sequences. so, the time complexity of hill sorting would be better than O (n^2). Because of the number of insertions, we know that one insert sort is stable and does not change the relative order of the same elements, but in different insertion sorts, the same elements may move in their own insert sort, and finally their stability will be disturbed, so the shell sort is unstable.

(8) Heap Sorting
We know that the heap structure is the child of node I for the 2*i and 2*i+1 nodes, The large top heap requires that the parent node is greater than or equal to its 2 child nodes, and the small top heap requires the parent node to be less than or equal to its 2 child nodes. In a sequence of n, the process of heap sequencing is to select the largest (large top Heap) or the smallest (small top heap) from the beginning of N/2 and its child nodes by a total of 3 values, and the choice between the 3 elements will of course not destabilize. But when for n/2-1, n/2-2, ... 1 When these parent nodes select an element, the stability is Broken. It is possible that the N/2 of the parent node Exchange passes the latter element, while the n/2-1 parent node does not exchange the subsequent same element, then the stability between the 2 identical elements is Destroyed. so, heap sorting is not a stable sorting algorithm

1 Quick Sort (QuickSort)

Quick Sort is an in-place sort, divide-and-conquer, large-scale recursive algorithm. essentially, It is the in-place version of the merge Sort. The quick sort can be made up of four steps below.

(1) If there is no more than 1 data, return directly.
(2) general selection of the leftmost value of the sequence as pivot Data.
(3) the sequence is divided into 2 parts, part of which is larger than the pivot data, and the other part is smaller than the pivot Data.
(4) use recursion to sort the series on both Sides.

Fast sorting is faster than most sorting algorithms. Although we can write algorithms that are faster than fast sorting in some special cases, there is usually no faster than that. Fast sorting is recursive, and it is not a good choice for machines with very limited memory.

2 Merge Sort (mergesort)

The merge sort first breaks down the sequence to be sorted, from 1 to 2, 2 to 4, and then to a group of 1, which can be sorted and then merged back into the original sequence so that all the data can be Sorted. A merge sort is slightly faster than a heap sort, but requires more memory space than the heap, because it requires an extra array.

3 heap sequencing (heapsort)

Heap sequencing is suitable for situations where data volumes are very large (millions of data).

Heap ordering does not require a large number of recursive or multidimensional staging arrays. This is appropriate for a very large sequence of data volumes. For example, more than millions of records, because the fast sort, the merge sort all uses the recursive design algorithm, when the data volume is very big, may have the stack overflow error.

The heap sort will build all the data into a heap, the largest data at the top of the heap, and then exchange the heap top data and the last data of the Sequence. Then rebuild the heap again, exchange the data, and then go down and sort all the Data.

4 Shell Sort (shellsort)

Shell sorting divides the data into groups, sorts each group first, and then inserts all the elements one at a time to reduce the number of times the data is exchanged and Moved. The average efficiency is O (nlogn). The rationality of grouping will have an important effect on the ALGORITHM. Now use the D.e.knuth grouping method more.

The shell sort is 5 times times faster than the bubble sort, twice times faster than the insertion sort. Shell sorting is much slower than quicksort,mergesort,heapsort. But it is relatively simple, it is suitable for the amount of data under 5000 and speed is not particularly important occasions. It is very good to repeat the sequence of numbers with a smaller amount of Data.

5 Insert Sort (insertsort)

Insert sort by inserting the values in the sequence into a sequence that is already sorted until the end of the Sequence. Insert sort is an improvement to the bubbling sort. It's twice times faster than bubble Sort. It is generally not necessary to use the insert sort when the data is greater than 1000, or to repeat the sequence of more than 200 data items.

6 Bubble Sort (bubblesort)

Bubble sort is the slowest sort algorithm. It is the least efficient algorithm in practical application. It compares each element in the array by one trip to another, so that the larger data sinks and the smaller data Bandi. It is an O (n^2) algorithm.

7 Interchange Sort (exchangesort) and select sort (selectsort)

Both of these sorting methods are sorting algorithms for the switching method, and the efficiency is O (n2). In the actual application is in and bubble sort basically same position. They are just the initial stages of the development of sorting algorithms, which are less used in practice.

8 Cardinal Sort (radixsort)

The Cardinal sort and the usual sorting algorithm do not go the same way. It is a relatively novel algorithm, but it can only be used for the ordering of integers, if we want to apply the same method to floating point numbers, we must understand the storage format of floating-point numbers, and in a special way to map floating-point numbers to integers, and then mapping back, This is a very troublesome thing, therefore, its use is not Much. and, Most importantly, This algorithm also requires more storage space.

9 Summary

Here is a general table that summarizes the characteristics of all of our common sorting algorithms.

Sorting method Average Time Worst case scenario Degree of stability Extra Space Note
Bubble O (n2) O (n2) Stability O (1) N Hours better
Exchange O (n2) O (n2) Not stable O (1) N Hours better
Choose O (n2) O (n2) Not stable O (1) N Hours better
Insert O (n2) O (n2) Stability O (1) Most are sorted better
Base O (logrb) O (logrb) Stability O (n)

B is the true number (0-9),

R is Cardinal number (1000)

Shell O (nlogn) O (ns) 1<s<2 Not stable O (1) S is the selected grouping
Fast O (nlogn) O (n2) Not stable O (nlogn) Better when N is big
Merge O (nlogn) O (nlogn) Stability O (1) Better when N is big
Heap O (nlogn) O (nlogn) Not stable O (1) Better when N is big

Time complexity of various sorting algorithms

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.