Sorting Algorithm Summary

Source: Internet
Author: User

1. Bubble Sorting

Bubble Sorting is a simple sorting method. The algorithm is as follows:
1. First, put all the numbers to be sorted into the work list.
2. from the first number in the list to the second to the last number, check one by one: if the number on a certain digit is greater than the next digit, It is exchanged with the next digit.
3. Repeat Step 2 (add 1 to the reciprocal number. For example, the first to the second to the last, the second to the third to the last, and so on until the second cannot be exchanged.
The C language is implemented as follows:

Int bubblesort (int * a, int B) // A is the integer array to be sorted, and B is the number of elements in the array to be sorted {int I, j, temp; for (j = 0; j <n-1; j ++) for (I = 0; I <n-1-j; I ++) {if (a [I]> A [I + 1]) // sort the array element size in ascending order {temp = A [I]; A [I] = A [I + 1]; A [I + 1] = temp ;}}}

Worst time complexity O (N & sup2 ;)
Optimal time complexity O (N)
Average time complexity O (N & sup2 ;)
Worst space complexity O (n) Total, O (1) Auxiliary
.
2. Insert sorting

Insert sorting is also a simple sorting method. The algorithm is as follows:
1. Starting from the first element, the element is already sorted.
2. Take the next element and scan it forward from the back in the sequence of sorted elements.
3. If the ordered sequence contains more elements than the new one, move the element to the right.
4. Repeat Step 3 until the sorted elements are less than or equal to the new elements.
5. Insert new elements in the current position.
6. Repeat Step 2.
Use C to implement the following:

Int insertsort (int * a, int B) {int I, j; int temp; for (I = 0; I </span> B; I ++) {temp = A [I]; for (j = I-1; j> = 0; j --) {if (a [J]> temp) A [J + 1] = A [J]; // move the element to the right. else {A [J + 1] = temp; break ;}}}}

Worst time complexity O (N & sup2 ;)
Optimal time complexity O (N)
Average time complexity O (N & sup2 ;)
Worst space complexity O (n) Total, O (1) Auxiliary
.

3. Select sorting

The sorting method is as follows:
1. Set n numbers to be sorted in the array memory. The array subscript starts from 1 and ends with N.
2. I = 1
3. Search for the smallest element from the I element of the array to the N element. (The specific process is: first set arr [I] to the smallest, compare one by one, and if there is a smaller ratio, the exchange)
4. Swap the smallest element found in the previous step with the I-th element.
5. If the I = n-1 algorithm ends, otherwise, return to step 1.
The C language is implemented as follows:

Int selectsort (int * a, int B) {int I, j; int flag; // minimum int temp used to record which element; for (I = 0; I </span> B; I ++) {flag = I; for (j = I + 1; j A [J]) {flag = J ;} // select the smallest element from I} temp = A [flag]; A [flag] = A [I]; A [I] = temp; // swap element }}

Worst time complexity (N & sup2 ;)
Optimal time complexity (N & sup2 ;)
Average time complexity (N & sup2 ;)
Worst space complexity (n) Total, O (1) Auxiliary

The time complexity of the preceding three sorts is O (N & sup2 ;).

4. Fast sorting

(A) sorting process:

(B) the whole process of sorting

Practice has proved that quick sorting is the most efficient of all sorting algorithms. It adopts the idea of divide and conquer: first ensure that the first half of the list is smaller than the second half, and then sort the first half and the second half separately, so that the whole list is ordered.
The basic algorithm for fast sorting is:
1. Pick out an element from the series, called a benchmark ),
2. Re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After this split, the benchmark is its final position. This is called a partition operation.
3. recursively sort the subseries smaller than the reference value element and the subseries greater than the reference value element.
At the bottom of the delivery, the number of columns is zero or one, that is, they are always sorted. Although it is always handed back, this algorithm always ends, because in each iteration, it will at least place an element at its final position.
The C language is implemented as follows:

Void swap (int * a, int * B) {int T = * A; * A = * B; * B = T;} int quicksort (int * a, int B) {int I, j; int base; If (B> 1) {base = A [0]; // set the first element as the baseline I = 1; j = B-1; while (I </span> J) {if (a [I] </span> base) I ++; else swap (& A [I], & A [j --]); // if the number of I positions is greater than the benchmark, move back} if (a [I] </span> Base) {// Insert the benchmark to the intermediate swap (& A [0], & A [I]); quicksort (A, I + 1 ); quicksort (& A [I + 1], b-i-1);} else {swap (& A [0], & A [I + 1]); quicksort (A, I ); quictsort (& A [I], B-I );}}}

The time complexity of fast sorting is O (nlogn), but the worst case is O (N & sup2 ;).
Worst time complexity (N & sup2 ;)
Optimal time complexity (nlogn)
Average time complexity (nlogn) comparisons
The worst space complexity varies depending on the implementation method.

 

5. Hill sorting is unstable.

A brief description of the algorithm concept:

In the direct insertion sorting algorithm, insert a number at a time to add only one node to the sequence,
This does not help insert the next number. If the comparison is relatively long distance (called
Incremental), so that when the number moves across multiple elements, a comparison may be eliminated.
Multiple elements are exchanged. D. L. Shell was implemented in the sorting algorithm named by him in 1959.
This idea. The number of groups to be sorted by the algorithm is divided into several groups according to an incremental D.
The subscript of the record is different. D. Sort all the elements in each group, and then use a small increment.
Sort it and then sort it in each group. When the increment is reduced to 1, the entire number to be sorted is divided
A group. The sorting is complete.

The following function is an implementation of a hill sorting algorithm. The first half of the sequence is incremental,
It will be halved each time until the increment is 1.

Hill sorting is unstable.

Input: array name (that is, the first address of the array), number of elements in the array

Void shell_sort (int * X, int N) {int H, J, K, t; for (H = n/2; h> 0; H = H/2) /* control increment */{for (j = H; j <n; j ++) /* this is actually the preceding direct insertion sorting */{T = * (x + J); For (k = J-h; (K> = 0 & T <* (x + k); k-= h) {* (x + K + H) = * (x + k );} * (x + K + H) = T ;}}}

 

6. Heap sorting
Input: array name (that is, the first address of the array), number of elements in the array

A brief description of the algorithm concept:

Heap sorting is a kind of tree-based sorting that effectively improves the Direct selection and sorting.
The heap is defined as follows: a sequence with n elements (H1, H2,..., HN ).
Meet (HI> = h2i, HI> = 2I + 1) or (Hi <= h2i, hi <= 2I + 1) (I = 1, 2 ,..., n/2)
It is called heap. Here we only discuss the heap that meets the conditions of the former.

From the definition of heap, we can see that the heap top element (that is, the first element) must be a heap top element. Full Binary Tree
Intuitively represents the heap structure. The heap top is the root, and the others are the left and right subtree.
Initially, we regard the sequence of numbers to be sorted as a binary tree for sequential storage, and adjust their storage order to make it a heap. At this time, the root node of the heap has the largest number. Then, the root node is exchanged with the last node in the heap. Then adjust the preceding (n-1) number to make it heap. Wait until there are only two nodes in the heap and exchange them. Finally, an ordered sequence of N nodes is obtained. According to the algorithm description, heap sorting requires two processes: creating a heap and switching the last element of the heap. Therefore, heap sorting consists of two functions. One is the heap build penetration function, and the other is to call the penetration function repeatedly.
The sorting function.

Heap sorting is unstable. Algorithm time complexity O (nlog2n ).


Function: penetration heap
Input: array name (that is, the first address of the array), number of elements involved in heap creation, starting from the first element

Void sift (int * X, int N, int s) {int T, K, J; t = * (x + S ); /* Temporary Start Element */k = s;/* Start Element subscript */J = 2 * k + 1; /* element subscript of the right subtree */while (j <n) {If (j <n-1 & * (x + J) <* (x + J + 1 )) /* determine whether the heap condition is met: if the heap condition is met, continue the next round of comparison; otherwise, adjust the condition. */{J ++;} If (T <* (x + J)/* adjust */{* (x + k) = * (x + J ); k = J;/* after adjustment, the starting element is also adjusted */J = 2 * k + 1;} else/* No need to be adjusted. It is already a heap, exit the loop. */{Break;} * (x + k) = T;/* place the Start Element in the correct position */}


Function: heap sorting
Input: array name (that is, the first address of the array), number of elements in the array

Void heap_sort (int * X, int N) {int I, K, T; int * P; for (I = n/2-1; I> = 0; I --) {sift (x, N, I);/* Initial heap creation */} For (k = n-1; k> = 1; k --) {T = * (x + 0);/* place the heap top to the end */* (x + 0) = * (x + k); * (x + k) = T; sift (x, K, 0);/* Create a heap after the remaining number */}}

 

Introduction and complexity analysis of several common sorting algorithms

Related Concepts

1. Stable sorting (stable sort) and non-stable sorting

Stable sorting means that all equal numbers can still maintain their relative order before sorting after some sort algorithm operation. The opposite is non-stable sorting.

2. Internal sorting and external sorting)

In the sorting process, all the numbers to be sorted are in the memory and their storage order is adjusted in the memory, called inner sorting. During the sorting process, only some of the numbers are transferred to the memory, the sort method for storing memory adjustment numbers in external storage is called external sorting.

 

Sorting Algorithm

[Bubble Sorting] (bubble sort)

The Bubble sorting method is the simplest sorting method. The basic idea of this method is to regard the elements to be sorted as vertical "Bubbles", and the smaller elements are relatively light, so as to move up and down. In the Bubble sorting algorithm, we need to process the "bubble" sequence several times. The so-called one-time processing is to check the sequence from the bottom up, and always pay attention to whether the order of two adjacent elements is correct. If the order of two adjacent elements is incorrect, that is, the "light" elements are located below, and their positions are exchanged. Obviously, after processing it again, the "lightest" element floated to the highest position; after processing it twice, the "minor" element floated to the lower position. During the second processing, you do not have to check because the element at the highest position is already the lightest element. In general, the I-th processing does not have to check the elements above the I-th high position, because they are sorted correctly after the processing of the previous I-1 times.

Bubble Sorting is stable. The algorithm time complexity is O (n2 ).


Selection sort)

The basic idea of sorting is to process the sequence of sorted records over n-1 times. The I-times processing is to swap the smallest of [I. N] With position I. In this way, after I times, the position of the previous I record is correct.

The sorting is unstable. The algorithm complexity is O (n2 ).

 

Insert sorting (insertion sort)

The basic idea of insertion sorting is that after I-1 processing, L [1 .. I-1] has arranged the order. The I-th processing only inserts l into the proper position of L [1 .. I-1], making L [1. I] A sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L and L [I-1], if l [I-1] ≤ l, then l [1 .. i] the order has been sorted, the I-times processing is over; otherwise the position of switching L and L [I-1] continues to compare L [I-1] and L [I-2], until a position J (1 ≤ j ≤ i-1) is found so that l [J] ≤ L [J + 1.

Direct insertion and sorting are stable. The algorithm time complexity is O (n2)

 

Heap Sort)

Heap sorting is a kind of tree-based sorting. During the sorting process, a [n] is considered as a Complete Binary Tree sequential storage structure, use the inner relationship between parent and child nodes in A Complete Binary Tree to select the smallest element.

Heap sorting is unstable. Algorithm time complexity O (nlog2n ).

 

[Merge sort] (merge sort)

The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence.

Merge Sorting is stable. The time complexity is O (nlog2n) in both the best and worst cases ).

 

Quick Sort)

Quick sorting is an essential improvement of Bubble sorting. The basic idea is that after scanning, the length of the sorting sequence can be greatly reduced. In Bubble sorting, A scan can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may be reduced by 1. By performing a quick sorting scan, you can make sure that the numbers on the left of a certain number (based on it) are smaller than that on it, and the numbers on the right are larger than that on it. Then, we use the same method to process the numbers on both sides of it until there is only one element on the left and right of the benchmark.

 

Fast sorting is unstable. Ideally, the algorithm time complexity O (nlog2n) and the worst O (N ^ 2 ).

 

Comparison of sorting methods

The time complexity of the Bubble Sorting Algorithm is O (n ^ 2)

The time complexity of the Sorting Algorithm is O (n ^ 2)

The time complexity of inserting sorting algorithms is O (n ^ 2)

Fast sorting is unstable. Ideally, the algorithm time complexity O (nlog2n) and the worst O (N ^ 2 ).

The time complexity of the heap sorting algorithm is O (nlogn)

The time complexity of the Merge Sorting Algorithm is O (nlogn)

 

1. Basic Concepts

1.1 stable sort and non-stable sorting

Stable sorting means that after some sorting method, all equal numbers can still maintain their relative order before sorting ,. On the contrary, it is a non-stable sorting.

For example, a group of numbers is sorted by A1, A2, A3, A4, and A5, where A2 is A4, Which is A1, A2, A4, A3, A5,

We can say that this sort is stable, because A2 is before A4, and it is still before A4. For example, A1, A4, A2, A3, and A5 are not stable.

1.2 internal sorting and external sorting)
In the sorting process, all the numbers to be sorted are in the memory and their storage order is adjusted in the memory, called inner sorting. During the sorting process, only some of the numbers are transferred to the memory, the sort method for storing memory adjustment numbers in external storage is called external sorting.

1.3 time complexity and space complexity of the algorithm

The time complexity of an algorithm refers to the computing workload required to execute an algorithm. The space complexity of an algorithm generally refers to the memory space required to execute this algorithm.


2. Several common algorithms

2.1 bubble sort)
The Bubble sorting method is the simplest sorting method. The basic idea of this method is to regard the elements to be sorted as vertical "Bubbles", and the smaller elements are relatively light, so as to move up and down. In the Bubble sorting algorithm, we need to process the "bubble" sequence several times. The so-called one-time processing is to check the sequence from the bottom up, and always pay attention to whether the order of two adjacent elements is correct. If the order of two adjacent elements is incorrect, that is, the "light" elements are located below, and their positions are exchanged. Obviously, after processing it again, the "lightest" element floated to the highest position; after processing it twice, the "minor" element floated to the lower position. During the second processing, you do not have to check because the element at the highest position is already the lightest element. In general, the I-th processing does not have to check the elements above the I-th high position, because they are sorted correctly after the processing of the previous I-1 times.

Bubble Sorting is stable. The algorithm time complexity is O (n2 ).

2.2 selection sort)
The basic idea of sorting is to process the sequence of sorted records over n-1 times. The I-times processing is to swap the smallest of L [I. N] with L. In this way, after I times, the position of the previous I record is correct.

The sorting is unstable. The algorithm complexity is O (n2 ).

2.3 insertion sort)
The basic idea of insertion sorting is that after I-1 processing, L [1 .. I-1] has arranged the order. The I-th processing only inserts l into the proper position of L [1 .. I-1], making L [1. I] A sorted sequence. To achieve this goal, we can use the sequential comparison method. First compare L and L [I-1], if l [I-1] ≤ l, then l [1 .. i] the order has been sorted, the I-times processing is over; otherwise the position of switching L and L [I-1] continues to compare L [I-1] and L [I-2], until a position J (1 ≤ j ≤ i-1) is found so that l [J] ≤ L [J + 1. Figure 1 demonstrates the insertion sorting process for the four elements, which requires three inserts: (a), (B), and (c.

Direct insertion and sorting are stable. The algorithm time complexity is O (n2)

2.4 heap sorting
Heap sorting is a kind of tree-based sorting. During the sorting process, a [n] is considered as a Complete Binary Tree sequential storage structure, use the inner relationship between parent and child nodes in A Complete Binary Tree to select the smallest element.

Heap sorting is unstable. Algorithm time complexity O (nlog N ).

2.5 Merge Sorting
There are two ordered (ascending) sequences stored in the adjacent positions of the same array. It may be set to a [L .. m], a [M + 1 .. h], merge them into an ordered series, and store them in a [L .. h].

Merge Sorting is stable. The time complexity is O (nlog2n) in both the best and worst cases ).

2.6 fast sorting
Quick sorting is an essential improvement of Bubble sorting. The basic idea is that after scanning, the length of the sorting sequence can be greatly reduced. In Bubble sorting, A scan can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may be reduced by 1. By performing a quick sorting scan, you can make sure that the numbers on the left of a certain number (based on it) are smaller than that on it, and the numbers on the right are larger than that on it. Then, we use the same method to process the numbers on both sides of it until there is only one element on the left and right of the benchmark.

Fast sorting is unstable. Ideally, the algorithm time complexity O (nlog2n) and the worst O (N ^ 2 ).

 

Summary of the stability, time complexity, and space complexity of various sorting:


Sorting Algorithm Summary

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.