Summary of sorting algorithms

Source: Internet
Author: User

Sort Time list:

name

Complexity of

Description

Notes

bubble sort
Bubble   Sort

o (n*n)

 

Insert Sort

insertion Sort

O (n*n)

remove elements from each other in the sequence of sequenced elements, and then scan them in the appropriate position

initially, the ordered sequence of elements is empty

Select Sort

O (n*n)

The smallest element is first found in the unordered sequence, placed at the start of the sort sequence, and then continues to look for the smallest element from the remaining unsorted elements and then to the end of the sort sequence. recursively.

 

quick sort

quick sort

o (n *log2 (n))

First select the middle value, then put the smaller than it on the left side, large on the right side (the concrete implementation is to find from both sides, find a pair after exchange). The process is then used separately on both sides (recursion).

 

o (n *log2 (n))

a sort algorithm constructed using the data structure of the heap (heaps). A heap is an approximate complete binary tree structure that satisfies the heap property at the same time: that is, the child node's key value or index is always less than (or greater than) its parent node.

approximate complete binary tree

Hill sort

shell

o (N1+£)

0<£<1

Select a step and then sort by the cell with the step in intervals. Recursively, the step size becomes smaller until it is 1.

&NBSP;

box sort
Bin sort

o (n)

set a number of boxes, the key is equal to the  k  records are all loaded into the first k  Box   (  assign  )  , and then sequentially link the non-empty boxes to the end of each other   (  collect  )  .

One of the assigned sorts: through   distribution     and     collection     process to achieve sorting.

Bucket Sort

Bucket Sort

O (n)

The idea of bucket sequencing is to divide [0, 1] into n equal-sized sub-ranges, each of which is a bucket.


Sorting Method

Average Time

Worst Case Scenario

Degree of stability

Extra Space

Notes

bubbling

O (N2)

O (N2)

Stable

O (1)

n hours better

Exchange

O (N2)

O (N2)

not stable

O (1)

n hours better

Select

O (N2)

O (N2)

not stable

O (1)

n hours better

Insert

O (N2)

O (N2)

Stable

O (1)

most are sorted better

cardinality

O (LOGRB)

O (LOGRB)

Stable

O (N)

B is the true number (0-9),R is the cardinality ( a 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)

Stable

O (1)

better when n is big

Heap

O (NLOGN)

O (NLOGN)

not stable

O (1)

better when n is big



The sorting is divided into four categories by average time:

(1) Order of Square (O (n2) )
generally referred to as simple sorting, such as direct insertion, direct selection, and bubbling sorting;

(2) Order of linear Logarithmic order (O (NLGN) )
such as fast, heap and merge sort;

(3) O (N1+£) Order ordering
£ is a constant between 0 and 1, i.e. 0<£<1, such as Hill sort;

(4) Order of Linear Order (O (n) )
sort as cardinality.

comparison of various sorting methods
simple sorting is best for direct insertion, fast sorting, and direct insertion and bubbling when the file is in the positive order.

factors that affect the sorting effect
because different sorting methods adapt to different application environments and requirements, the following factors should be taken into consideration when choosing the appropriate sorting method:
① number of records to be sorted n;
the size of the ② record (size);
the structure of the ③ keyword and its initial state;
④ the requirement of stability;
⑤ The conditions of the language tools;
⑥ storage structure;
⑦ time and the complexity of the auxiliary space.

the choice of sorting method under different conditions

(1) If n is small (for example, n≤50), direct insertion or direct selection can be used.
when the record size is small, the direct insertion sort is better, otherwise, because the number of directly selected moving records is less than the direct interpolation, it is advisable to select the direct selection sort.
(2) If the initial state of the document is basically ordered (order), it should be used to directly plug, bubble or random quick ordering is appropriate;
(3) If n is larger, it should be sorted by the time complexity O (NLGN): Quick sort, heap sort, or
merge sort.
fast Sorting is currently considered the best method based on the comparison of the internal sorting, and when the keywords to be sorted are randomly distributed, the average time for fast sorting is shortest;
heap ordering requires less than a quick sort of secondary space, and there is no worst-case scenario in which a quick sort can occur. Both of these sorts are unstable.

If order stability is required, merge sort is optional. But sorting algorithms that are 22 merged from a single record are not worth advocating, and they can often be combined with direct insert sorting. The long ordered sub-file is obtained by using the direct insert sort, and then 22 is merged. Since the direct insert sort is stable, the improved merge sort is still stable.


--------------------------------------------------------------The analysis of sort stability, Helps to understand and master--------------------------------------------

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.

Second, describe the benefits of stability. If the sorting algorithm is stable, sort from one key and then sort from another, the result of sorting the first key can be used to sort the second key. The Cardinal sort is like this, first by the low sort, successively by the high order, the low-level same element its sequence is also the same time will not change. In addition, if the sorting algorithm is stable, the number of elements exchanged may be less (personal sense, not confirmed) for comparison-based sorting algorithms.

Back to the topic, now analyze the stability of the common sorting algorithms, each giving a simple reason.

(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 (5th element, subscript from 1) The exchange will be the stability of element 3 is disturbed, so fast sorting is an unstable sorting algorithm, Instability occurs at the moment of the exchange of central elements and a[j].

(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. Therefore, heap sorting is not a stable sorting algorithm.

Summary of 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.