Some ideas of sorting algorithm

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] and 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, the number of comparisons is known;

Disadvantage: Slow, can only move the adjacent two data at a time, the number of mobile data more.

Second, choose the 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[1] and a[3], and if A[1] is greater than a[3, the values of both are exchanged, otherwise they will not change. Compare a[1] with a[4], and so on, and finally compare the values of a[1] and A[n]. After this process, the value of a[1] must be the smallest in this set of data. A[2] and a[3]~a[n] are compared in the same way, the value of a[2] must be the smallest of a[2]~a[n]. A[3] and a[4]~a[n] are compared one round in the same way, and so on. A[1], a[2] 、...... A[n] are arranged in ascending order after the n-1 round is processed.

Advantages: Stable, compared with the bubble sort, the number of data movement is less than bubble sort;

Cons: Relative or slow.

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 group A, b[1] as an array of n=1 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 known as Hill sort.

You know a set of unordered data a[1], a[2] 、...... a[n], you need to sort them in ascending order. 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, and so on, 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.

After a period of learning and programming, I have a few of the above methods of sequencing proficiency or understanding. On this basis, through my thinking and practice, I have developed a new sorting algorithm: segmented insertion sort.

Segmented 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. The array A is divided into x equal parts (x<<n), with n/x data for each equal. Store the first data of each paragraph in array c: c[1], c[2] 、...... c[x]. Use the Insert sort to manipulate the data in array B. When inserting, B compares with C, determines which paragraph of B is in a, and then inserts b into the corresponding segment in a. As the data is inserted, the length of each paragraph in a varies, so after each insertion, the standard deviation s for the amount of each piece of data is detected, and a is re-segmented when it is greater than a certain value. When the amount of data is particularly large, it is possible to increase the speed by comparing the first data in each segment of a with that of the primary segment, and then the first data of the sub-segment.

Advantages: Fast, less than the number of comparisons;

Disadvantage: not suitable for less data sorting, s critical value can not be accurately learned, only by experience.

I designed the algorithm may be better than some algorithms, but it also has its advantages, disadvantages and scope of application. Not only is the sorting algorithm so, any algorithm is the same. No one is doing the best to say that their algorithm is. The process of designing a new algorithm is to increase its advantages, reduce its shortcomings and widen its scope of application. My most advocating sentence is: "No best, only better." ”

Some ideas of sorting algorithm

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.