Advantages and disadvantages of various sorting algorithms

Source: Internet
Author: User

First, bubble sort
You know a set of unordered data a[1], a[2] 、...... a[n], you need to sort them in ascending order. The value of a[1] and a[2] is compared first, if A[1] is greater than a[2, the value of both is exchanged, otherwise it is unchanged. Then compare the values of a[2] and a[3], and if a[2] is greater than a[3, the values of both are exchanged, otherwise they will not change. Compare A[3] with a[4], and so on, and finally compare the values of a[n-1] with A[n]. After this process, the value of a[n] must be the largest in this set of data. Again to a[1]~a[n-1] in the same way, the value of a[n-1] must be the largest of a[1]~a[n-1]. Again to A[1]~a[n-2] the same method of processing a round, and so on.   A[1], a[2] 、...... A[n] are arranged in ascending order after the n-1 round is processed. Advantages: stable;
Cons: Slow, only two adjacent data can be moved at a time. Second, choose the sort
Each trip selects the smallest (or largest) element from the data element to be sorted, placed in the final order of the ordered sequence, until all the data elements are sorted out.
Select Sort is an unstable sort method.
The direct selection sort of n records files can be sorted by N-1 direct selection order to get ordered result: ① initial state: Unordered area is R[1..N], ordered area is empty. ② 1th Trip Sort
In the unordered area R[1..N], select the smallest record of the keyword R[k], and the 1th record of the unordered zone r[1] exchange, so that r[1..1] and R[2..N] respectively to increase the number of records added a new ordered area and the number of records reduced by 1 new unordered area. ......
③ Order of the first I trip
At the beginning of the sequence I, the current ordered and unordered areas are r[1..i-1] and R (1≤i≤n-1) respectively. The sequencing selects the smallest record of the keyword from the current unordered area R[k], swapping it with the 1th record R in the unordered zone, so that r[1..i] and R change to a new unordered area with a new ordered area and a number of records with a decrease of 1 respectively.
In this way, the direct selection of the files of N records can be sorted by n-1 direct selection to get an ordered result.   Advantages: The number of times to move data is known (n-1 times), disadvantage: more times. Third, insert sort
A set of ascending data is known a[1], a[2] 、...... A[n], a set of unordered data b[1], b[2] 、...... b[m], which need to be merged into an ascending sequence. First compare the value of b[1] and a[1], if B[1] is greater than a[1], then skip, compare b[1] and a[2], if B[1] is still greater than a[2], then continue to skip until b[1] is less than a data in an array a[x], then a[x]~a[n] Move backward one bit, will b[ 1] inserted into the original a[x], this completes the insertion of the b[1]. B[2]~B[M] is inserted in the same way. (If countless groups of a, can be b[1] as the n=1 array a) Advantages: stable, fast;
Disadvantage: The number of comparisons is not necessarily, the less the number of comparisons, the insertion point after the data movement more, especially when the total amount of data, but with a linked list can solve the problem.
Four, reduce the incremental sort
Proposed by Hill in 1959, also called Hill sort (shell sort).
You know a set of unordered data a[1], a[2] 、...... a[n], you need to sort them in ascending order. It is found that when n is not big, the effect of inserting sort is good. First Take one increment D (d<n), will a[1], A[1+d], a[1+2d] ... Listed as the first group, a[2], a[2+d], a[2+2d] ... Listed as second group ..., A[d], a[2d], a[3d] ...   Listed as the last group by the second analogy, in each group with the insertion sort, and then take the d ' <d, repeat the above operation until d=1. Advantages: Fast, less data movement;
Disadvantages: Instability, the value of D is how much, should take a number of different values, can not know exactly, only by experience to take. V. Quick Sort
Quick Sort is an improved version of the bubbling sort, the fastest sorting method known at the moment.
You know a set of unordered data a[1], a[2] 、...... a[n], you need to sort them in ascending order. Take data a[x] as a benchmark first. Compare A[x] with other data and sort, so that a[x] is ranked in the K-bit of the data, and each data in a[1]~a[k-1] is <a[x],a[k+1]~a[n] >a[x], and then the strategy of the division is respectively A[1]~a[k-1]   and A[k+1]~a[n] Two sets of data for quick sorting.   Advantages: Very fast, less data movement; Cons: Unstable. Six, Box sorting
It is known that a set of unordered positive integer data a[1], a[2] 、...... a[n] must be sorted in ascending order.   First define an array x[m], and m>=a[1], a[2] 、...... a[n], and then loop n times, each x[a]++. Advantages: Fast, efficiency up to O (1)
Disadvantage: The data range must be a positive integer and smaller


Require.async ([' wkcommon:widget/ui/lib/sio/sio.js '], function (sio) {var url = ' Https://cpro.baidustatic.com/cpro/ui /c.js '; Sio.callbybrowser (URL, function () {baidu_clb_fillslotasync (' u2845605 ', ' cpro_u2845605 ');}); });


void function (E,t) {for (var n=t.getelementsbytagname ("img"), A=+new date,i=[],o=function () {This.removeeventlistener &&this.removeeventlistener ("Load", o,!1), I.push ({img:this,time:+new Date})},s=0;s< n.length;s++)! function () {var e=n[s];e.addeventlistener?! E.complete&&e.addeventlistener ("Load", o,!1): E.attachevent&&e.attachevent ("onReadyStateChange", function () {"Complete" ==e.readystate&&o.call (E,o)})} (); Alog ("Speed.set", {fsitems:i,fs:a})} (window, Document);

Vi. Merge Sort
Merge sort is the merging of two or more two ordered tables into a new ordered table multiple times. The simplest merge is to combine two ordered sub-tables directly into an ordered table.

Merge sort is a stable sort. That is, the order of equal elements does not change. such as input records 1 (1) 3 (2) 2 (3) 2 (4) 5 (5) (in parentheses are recorded keywords) the output of 1 (1) 2 (3) 2 (4) 3 (2) 5 (5) in 2 and 2 is in the order of entry. This pair to sort the packets It is important to have more information to sort by one of the information, and to require other information to be arranged in the order in which they are entered. This is where it is more advantageous than the quick sort.

Merge sort: Merge sort is a non-in-place sort that will require as much secondary space as the sequence to be sorted. Using it to merge two ordered sequences will have unparalleled advantages. Its time complexity is O (nlog2n) in both the best and worst-case scenarios. Not sensitive to the order of the data. If data node data is large, it will not be suitable. But you can change the index operation, the effect will be very good.

Heap ordering: Because it is based on the direct selection of the order of the use of comparative results formed. The efficiency is greatly improved. It completes the sort by the total number of comparisons to O (nlog2n). It is an algorithm that is insensitive to the order of data. But the heap sort will need to do two steps:-is to build the heap, and the second is to sort (adjust the heap). Therefore, it is not suitable for small-scale sequences, but it will show superior performance for larger sequences.

Advantages and disadvantages 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.