I. Quick sorting
The main idea of quick sorting is "divide and conquer", that is, dividing a big problem into several small problems to solve them separately. First, select a "representing dollar" from the data to be sorted, and then place the elements smaller than the representing dollar on the left side of the array, and the larger one on the right side of the array. Take the left and right parts into consideration and perform the same processing on them, that is, select the representative yuan from the left and right sides, and then divide them. Therefore, a major step of fast sorting is to re-sort the Array Based on the representative elements, which is temporarily called partition (). The Quick Sort code is as follows:
#include <stdio.h>#include <stdlib.h>#include <string.h>int partition( int head, int tail, int array[] ){int rep = array[tail];int tail2 = tail;while ( head < tail ){while( array[head] <= rep && head < tail ){head++;}while ( array[tail] >= rep && head < tail ){tail--;}if ( head < tail ){int tmp = array[tail];array[tail] = array[head];array[head] = tmp;head++;tail--;}}array[tail2] = array[head];array[head] = rep;return head;}void quicksort( int head, int tail, int array[] ){int i;if ( tail <= head ){for ( i = 0; i < 6; i++ )printf( "%d", array[i] );printf( "\n" );return;}else{int q = partition( head, tail, array );//printf( "%d\n", q );quicksort( head, q - 1, array );quicksort( q + 1, tail, array );}}int main(){int array[6] = {1,2,3,5,4,2};//int a = partition( 0, 1, array );quicksort( 0, 5, array );int i;for ( i = 0; i < 6; i++ )printf( "%d ", array[i] );//printf( "%d", a );return 0;}
Then, consider the complexity. If the element is properly selected to make every array Division semi-open, the complexity is T (n) = 2 * T (n) + n, that is, O (n * logn), but in the worst case, if every selection on behalf of the Yuan makes the array seriously "biased", then the complexity is T (n) = T (n-1) + n, and the final complexity is O (n ^ 2 ). Therefore, fast sorting is good, but not stable enough. In addition, fast sorting requires an extra space of O (logn) for storing table elements in the recursive operation era.
Ii. Heap sorting
A heap is actually a Complete Binary Tree. The definition of the maximum heap is that the parent node is larger than two subnodes, and the minimum heap is similar. The heap sorting algorithm utilizes one of the largest heap attributes: the parent node in the heap must be the largest. The sorting algorithm has two subfunctions: maxheapify () and buildmaxheap (). maxheapify () combines a node and two Subtrees that are already the largest heap into one largest heap, buildmaxheap () is used to form an array into a maximum heap. The Code is as follows:
#include <stdio.h>#include <stdlib.h>void maxheapify( int array[], int root, int n ){int large;int tmp;if ( 2 * root + 1 > n -1 )return;else if ( 2 * root + 1 == n - 1){if ( array[2 * root + 1] > array[root] ){tmp = array[2 * root + 1];array[2 * root + 1] = array[root];array[root] = tmp;return;}return;}if ( array[2 * root + 1] < array[2 * root + 2] )large = 2 * root + 2;elselarge = 2 * root + 1;if ( array[root] < array[large] ){tmp = array[root];array[root] = array[large];array[large] = tmp;maxheapify( array, large, n );}return;}void buildmaxheap( int array[], int n ){int i;for ( i = (n - 2) / 2; i >= 0; i-- ){maxheapify( array, i, n );}return;}void heapsort( int array[], int n ){int i, tmp, j;buildmaxheap( array, n );for ( i = n - 1; i >= 0; i-- ){tmp = array[i];array[i] = array[0];array[0] = tmp;maxheapify( array, 0, i );}}int main(){int array[7] = {1, 3, 2, 5, 4,2,7};int i;heapsort( array, 7 );for ( i = 0; i < 7; i++ )printf( "%d ", array[i] );return 0;}
Complexity analysis: the complexity of maxheapify () is O (logn), buildmaxheap () is O (n * logn), and the total complexity is O (n * logn.
Iii. Insert sorting
Imagine the process of finding a card when playing poker. You can take a card in your left hand and a card in your right hand. After each card is obtained, insert the card into the proper position in the left hand card and arrange it in ascending order. This is also true for an array. it traverses the array from left to right and inserts each element to a proper position before it. The Code is as follows:
#include <stdio.h>#include <stdlib.h>void insertsort( int array[], int n ){int i, j, p;for ( i = 0; i < n; i++ ){p = array[i];for ( j = i - 1; j >= 0; j-- ){if ( p < array[j] ){array[j + 1] = array[j];array[j] = p;}elsebreak;}}return;}int main(){int i;int array[5] = {1,2,5,4,3};insertsort( array, 5 );for ( i = 0; i < 5; i++ ){printf( "%d ", array[i] );}return 0;}
The complexity is O (n ^ 2 ).
Iv. Bubble Sorting
Not much ~
V. Merge Sorting
Suppose there are two groups of playing cards in order. The order of these two sets of playing cards is a merge () process, and mergesort () is a recursive process, the Code is as follows:
#include <stdio.h>#include <stdlib.h>void merge( int array[],int p, int q, int r ){int i = p;int j = q;int k = 0;int * array_res = (int *)malloc( sizeof( int ) * (r - p + 1) );while ( i <= q - 1 && j <= r ){if ( array[i] <= array[j] ){array_res[k] = array[i];//printf( "%d", array_res[k] );//return;i++;k++;}else if ( array[i] > array[j] ){array_res[k] = array[j];j++;k++;}}if ( i > q - 1 ){while ( k < r - p + 1 ){array_res[k] = array[j];k++;j++;}}for ( i = 0; i < r - p + 1; i++ ){array[p + i] = array_res[i];}return;}void mergesort( int array[], int m, int n ){if ( m == n )return;else{int r = ( n + m ) / 2;mergesort( array, m, r );mergesort( array, r + 1, n );merge( array, m, r + 1, n );}}int main(){int array[6] = {2,2,3,2,4,6};mergesort( array, 0, 5 );int i;for ( i = 0; i < 6; i++ )printf( "%d ", array[i] );return 0;}
The time complexity is O (n * logn), and the extra space complexity is O (n ).
This article is from the "sdu_IS" blog, please be sure to keep this source http://hychuanshuo.blog.51cto.com/2724628/1274647