Advantages and disadvantages of various sorting algorithms

Source: Internet
Author: User

 

Advantages and disadvantages of sorting
Classification: Comprehensive Technology
Reprint program life 23:25:20

I. Bubble Sorting

A group of unordered data a [1], a [2],... A [n], which needs to be arranged in ascending order. First, compare the values of a [1] and a [2]. If a [1] is greater than a [2], the values of the two are exchanged. Otherwise, the values remain unchanged. Compare the values of a [2] and a [3]. If a [2] is greater than a [3], the values of the two are exchanged. Otherwise, the values remain unchanged. Compare the values of a [3] and a [4], and so on. Finally, compare the values of a [n-1] And a [n. In this case, the value of a [n] must be the largest in this set of data. Then, for a [1] ~ If a [n-1] processes a round in the same way, the value of a [n-1] must be a [1] ~ The largest value in a [n-1. Then, for a [1] ~ A [N-2] handles a round in the same way, and so on. Processing of n-1 round after a [1], a [2],... A [n] is arranged in ascending order.

Advantages: Stability;

Disadvantage: slow. Only two adjacent data can be moved at a time.

Ii. Select sorting

The sort version of the Bubble sorting.

Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged.

Selecting sorting is an unstable sorting method.

The Direct selection and sorting of files with N records can be directly selected and sorted through n-1 rows to get the ordered results:

① Initial status: the disordered area is R [1. N], and the ordered area is empty.

② Sorting by 1st bits

In the unordered zone R [1 .. n] selects the record R [k] with the minimum keyword, swaps it with the 1st records R [1] In the unordered area, so that R [1 .. 1] and R [2 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count.

......

③ Sort by I

At the beginning of the I-th sorting, the current ordered and disordered areas are R [1 .. I-1] and R (1 ≤ I ≤ N-1), respectively ). This sort selects the record with the smallest keyword from the unordered area.
R [K], swap it with the 1st records of the unordered zone R, so that R [1 .. i] and R change to a new ordered area with one more record count and a new unordered area with one fewer record count.

In this way, the direct sorting of files with N records can be directly selected through n-1 to obtain the ordered results.

Advantage: the number of moving data is known (n-1 );

Disadvantage: the number of comparisons is large.

Iii. Insert sorting

A group of sorted data in ascending order is known: A [1], a [2],… A [n], a group of unordered data B [1], B [2],... B [m], which must be combined into an ascending sequence. First, compare the values of B [1] and a [1]. If B [1] is greater than a [1], Skip, compare the values of B [1] and a [2]. If B [1] is still greater than a [2], Skip, until B [1] is smaller than a data in array a [X], a [x] ~ A [n] moves one digit backward, and inserts B [1] to the original position of a [X]. This completes the insertion of B [1. B [2] ~ B [m] is inserted in the same way. (If there is no array A, you can regard B [1] as an array a with n = 1)

Advantages: stable and fast;

Disadvantage: the number of comparisons is not certain. The less the number of comparisons, the more data is moved after the inserted point. Especially when the total amount of data is large, the linked list can solve this problem.

Iii. Reduce incremental sorting

Proposed by hill in 1959, also known as shell sorting ).

A group of unordered data a [1], a [2],... A [n], which needs to be arranged in ascending order. It is found that insertion sorting works well when n is not large. First, take an incremental D (D <n) and convert a [1], a [1 + D], a [1 + 2d]… Column A [2], a [2 + D], a [2 + 2d]… Column 2 ......, A [d], a [2D], a [3D]... Column is the last group, and so on. insert and sort in each group, and then take d' <D. Repeat the preceding operation until D = 1.

Advantages: fast, less data movement;

Disadvantages: unstable, the number of values of D, and the number of different values of D cannot be determined, but can only be obtained based on experience.

Iv. Quick sorting

Quick sorting is the fastest sorting method currently known.

A group of unordered data a [1], a [2],... A [n], which needs to be arranged in ascending order. First, use data a [x] as the benchmark. Compare and sort a [x] with other data so that a [x] is ranked at the K bit of the data and a [1] ~ Each data in a [k-1] <A [X], a [k + 1] ~ Each data in a [n] is> A [X], and then a [1] to A [k-1] And a [k + 1] ~ A [n] groups of data are sorted quickly.

Advantage: extremely fast, with less data movement;

Disadvantage: unstable.

V. Box sorting

A group of unordered positive integers A [1], a [2],... A [n], which needs to be arranged in ascending order. First define an array X [m], and m> = A [1], a [2],... A [n], followed by N cycles, X [a] ++ each time.

Advantages: fast and efficient O (1)

Disadvantage: The data range must be a positive integer and smaller.

Vi. Merge Sorting

Merging and sorting is to merge two or more ordered tables into a new ordered table multiple times. The simplest merge operation is to directly merge two ordered sub-tables into an ordered table.

Merge Sorting is a stable sorting. that is, the order of equal elements will not change. for example, if the input record 1 (1) 3 (2) 2 (3) 2 (4) 5 (5) (the record keyword is in parentheses ),
1 (1) 2 (3) 2 (4) 3 (2) 5 (5) 2 and 2
In the input order. this is important when sorting data that contains multiple pieces of information, and sorting other pieces of information according to the input order as much as possible. this is also the advantage of quick sorting.
 

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.