8 Common sorting algorithms

Source: Internet
Author: User

Part of the transfer from the internet ...

The following 8 kinds of sorting are internal sorting, both in memory, mainly from the theoretical aspects of the analysis.

Insert Sort

① Direct Insert Sort

Example: Six number 12 15 9 20 6 31 24 Use the direct insertion sort, such as:

Ideas:

The first step: from the given six number, casually take out a number, such as 12, to form an ordered data series (a number of course is ordered data series, do not look at the number of 12, when the other number does not exist );

The second step: Pick a number from the remaining five numbers , such as 15, and just 12 for comparison, 12<15, therefore, placed in the back of 12, forming a data series;

The third step: Select a number from the remaining four numbers, such as 9, and just ordered data series 12 15 For comparison, 9 < < 15, therefore, placed at the front, forming a data series 9 12 15;

The nth step, after inserting and contrasting such a single one, forms the sort result shown. When an element is inserted, it is first compared to the largest element in the data series, and if it encounters the same, it is placed behind the same element.

Characteristics:

Because of the need to constantly insert, so the direct insertion of the order is generally used in the chain list structure, belong to a stable sort.

② Hill (Shell) sort

is the improvement of the direct insertion sort, example: 10 numbers 57 68 59 52 72 28 96 33 24 19 in the order of Hill, such as:


Ideas:

The first step is to divide the number of sorted numbers by 2, and take the odd number to get the step (increment) D1 = 5;

The second step: according to the step D1, the 10 numbers are divided into five groups, and each of the five groups is directly inserted into the order;

The third step: Use the step D2 to continue dividing by 2, take the nearest odd number to get step d2=3;

Fourth step: According to the step D2, the 10 numbers are divided into three groups, and the five groups are directly inserted in the order;

Step N: Repeat the above grouping and sorting operations until the step becomes 1, that is, all records are placed in a group until they are sorted.

Characteristics

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.

Select sort

① Simple Selection sorting

Example: Four number 57 68 59 52 Select a sort:

Ideas:

The first step: from four number to find the smallest, and the initial state in the first position of the mobile interchange;

The second step: from the remaining three numbers to find the smallest, and the initial state in the second position of the mobile interchange;

Step N: Repeat the steps above to find the minimum and interchange until the last one.

Characteristics

For example, sequence 5 8 5 2 9, we know the first time to select the 1th element 5 will be exchanged with 2, then the original sequence of two 5 of the relative sequence is destroyed, so the selection is not a stable sorting algorithm.

② Heap Sequencing (image examples are from quanquanfly)

If you do not know the heap and characteristics can be in my previous blog "Common data structure-several special two-fork tree", about the heap sort, some complex, the following examples: example: for the sequence {12,56,23,26,15,86,92,75,65} to build a large top heap (Dagen), the initial heap is?

Ideas:

Divided into two large processes, the first is the construction of the heap process ;


Ideas:

The first step: according to the complete binary tree arrangement given the number, such as the first figure;

The second step: because the tree is a one-way process, the leaf node is unable to know the parent node, so can not take the leaf node to compare with the parent node, according to the node I,I>=N/2 as the characteristics of the leaf node, find the last non-leaf node, and then take it and its leaf knot point for comparison, if the leaf knot is smaller, It is interchangeable (building a large top pile), and vice versa.

The third step: immediately after adjusting the N/2-1th number node (Find out N/2-1 = 3, that is, 23rd node), see 23rd nodes of two nodes are larger than it, then select one of the largest exchange.

Step N: According to the above method, in turn interchange, and finally set up a large heap.

The second is the heap ordering process:


Ideas:

First step: The root node 92 is output according to the initial heap established above, and then the maximum number node is replaced by the largest node 65 instead of the root node.

Step two: After the previous step is completed, check whether the 65 nodes meet the requirements of the large top heap, and if they do not conform to the process of building the heap again (refer to the first procedure ).

Step N: According to the above two steps, repeated operation, you will get the desired results.

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

Exchange sort

① Bubble Sort

Comparison of common sorting algorithms. To illustrate:


Ideas:

The first step: compare the penultimate and penultimate numbers, if they are small, then swap;

The second step: compare the penultimate and the penultimate numbers, if they are small, then swap;

Step N: Filter out the smallest number and then repeat it from the rest of the numbers to get the sequence you want.

Characteristics

If two equal elements are not adjacent, then even if two are adjacent by the preceding 22 interchange, this is not exchanged at this time, so the order of the same elements is not changed, so bubble sort is a stable sorting algorithm.

② Quick Sort

The quick sort (divide-and-conquer idea) is an improvement of the bubble sort, the idea: to pick an element from the sequence, called the "Datum" (pivot), reorder the columns, all the elements are placed in front of the datum in a smaller position than the base value, and all the elements are positioned behind the datum (the same number can be either) At the end of the split, the benchmark is in the middle of the sequence. This is called a split (partition) operation; recursively (recursive) sorts sub-columns that are smaller than the base value elements and sub-columns that are larger than the base value elements.

Ideas:

The first step: Find a datum from the given sequence, 57, the left pointer to 57, and the right pointer to the last element;

The second step: compare the left pointer to the element pointed to by the right pointer, the right pointer to the element 19 than the left pointer to the element 57 small (datum), interchange position;

The third step: left pointer to right one followed by right pointer contrast, 68>57, so interchange;

Step N: Follow the above steps through the constant movement of the pointer and the contrast of the elements of the interchange, and finally the first 57-centered sequence (the left is less than 57, the right is greater than 57); then the recursion is used to sort the 57 before and after.

Characteristics

The dynamic diagram on the right of the above is a good illustration of the idea of a quick sort, and a fast sort is an unstable sorting algorithm.

Merge sort

This algorithm is a very typical application of the partition method (Divide and Conquer).


Ideas:

The first step: treat each element to be sorted as an ordered table (then by n ordered table), by 22 merge, generate? N/2? an ordered table with a length of 2 (the last table may be less than 2 in length).

The second step: the internal order of each group;

The third step: two groups of two groups to merge, the two pointers are respectively set to the minimum of two groups of two numbers, and then compare, small pick out, the pointer moves back, continue to compare.

Step N: In the above-mentioned constant merging and comparison, finally arrive at a correct sequence.

Merge sort is a stable sorting algorithm

Base sort

The principle of cardinality sorting is to cut the number of integers into different numbers and then compare them by the number of bits.


Ideas:

The first step is to collect the bits of the given sequence elements and then follow, put them in the corresponding position (0-9 sequence), and form a sequence according to the size of the single-digit discharge.

The second step: collect 10 bits, according to the sequence produced by the first step to the corresponding position, the formation of a new sequence;

The third step: collect the Hundred, according to the sequence produced by the second step to the corresponding position, to form the desired result sequence.

Characteristics

Base sorting is based on sorting separately and is collected separately, so it is a stable sorting algorithm

Comparison of various algorithms


8 Common 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.