merge Sort (merge-sort) is an efficient sorting algorithm based on merging operation, which is a very typical application of divide-and-conquer method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.
(If the reader is not familiar with what is called division, you can go to the 12th chapter of the introduction to algorithms.) )
The merge process is: Compare the size of a[i] and a[j], if A[I]≤A[J], then the first ordered table elements a[i] copied to r[k], and I and K plus 1; otherwise, the second ordered TABLE element A[j] copied to R[k], and the J and K respectively add 1, This loop continues until one of the ordered tables is finished, and then the remaining elements in the other ordered table are copied to the cells in R from subscript K to subscript t. Merging sorting algorithm we usually use recursive implementation, first to sort the interval [s,t] to the midpoint of the two points, then the left sub-range, then the right sub-range is sorted, and finally the left and right intervals with a merge operation into an orderly interval [s,t].
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/80/F9/wKioL1dFtx3St-_hAAEZNaX-y2Q525.jpg "title=" 1346230675_7613.jpg "alt=" Wkiol1dftx3st-_haaeznax-y2q525.jpg "/>
The code is as follows:
Void merge (Int* a,int left, int mid, int right, int* tem) {assert ( a); assert (TEM); int i = 0;int indix = mid+1;int begin = left;while (Begin <= mid && indix <= right) {if (A[begin] > a[indix]) tem[i++] = a[indix++];elsetem[i++] = a[begin++];} while (Begin <= mid) tem[i++] = a[begin++];while ( indix <= right) Tem[i++] = a[indix++];for (int j = 0; j < i; j++) Re-assign the sorted array to a{a[j+left] = tem[j];}} Void mergsort (Int* a,int left, int right,int* tem) {assert (a); assert (TEM); Int mid;if (left < right) {mid = (left + right)/2;mergsort (A,left,mid,tem) ; Mergsort (A,mid+1,right,tem); Merge (A,left,mid,right,tem);}} Bool mergesort (int* a, int n) &NBsp; { assert (a); int *p = new int[n]; opens up a space as large as a to store sorted data. if (p == null) return false; mergsort (A,&NBSP;0,&NBSP;N&NBSP;-&NBSP;1,&NBSP;P); delete[] p; return true; }
The above is a recursive algorithm
Since there is a recursive algorithm, there is a non-recursive algorithm
Void merge_sort (int * a, int length) {assert (a); int i, left_min, left_max, right_min, right_max, next; int *tmp = (int*) malloc (sizeof (int) * length); if (Tmp == NULL) { printf (" error: out of memory\n "); } for (i = 1; i < length; &NBSP;I&NBSP;*=&NBSP;2) for (left_min = 0; left_min < length - i; left_min = righT_max) { right_min = left_max = left_ min + i; right_max = left_max + i ; if (right_max > length) right_max = length; next = 0; while (left_min < left_max && right_min < right_max) tmp[next++] = a[left_min] > a[right_min] ? a[right_min++] : a[left_min++]; while (Left_min < left_max) a[--right_min] = a[--left_max]; while (next > 0) a[--right_min] = tmp[--next]; } free (TMP); }
Thus it can be seen that recursion and non-recursion are consistent in thought, but the method of realization is different.
The time complexity is O (NLOGN), which is the best, worst, and average time performance of the algorithm.
Spatial complexity of O (n)
The number of comparison operations is between (NLOGN)/2 and Nlogn-n + 1.
The number of assignment operations is (2NLOGN). The spatial complexity of the merging algorithm is: 0 (N)
Merge sort uses memory, but it is an efficient and stable algorithm.
Count sort
The Count sort is a non-comparison based sorting algorithm, which was proposed by Harold H. Seward in 1954. Its advantage is that when sorting integers within a certain range, its complexity is 0 (n+k) (where K is the range of integers), faster than any comparison sorting algorithm.
Algorithmic Thinking Editor
The count sort has additional restrictions on the data entered:
1, the elements of the input linear table belong to the finite partial sequence set S;
2. The length of the input linear meter is n,| S|=k (indicates that the total number of elements in the collection S is k), then K=o (n).
Under these two conditions, the complexity of the count ordering is O (n).
The basic idea of counting sorting is to determine the number of elements in the sequence that have a value less than x for each element in the given input sequence (this is not a comparison of the size of each element, but a count of the value of the element and the summation of the count value). Once you have this information, you can store the x directly in the correct position of the final output sequence. For example, if the value of only 17 elements in the input sequence is less than the value of x, then x can be stored directly in the 18th position of the output sequence. Of course, if more than one element has the same value, we cannot put these elements in the same position in the output sequence, so the above scenario should be modified as appropriate.
Algorithmic process editing
Suppose the input linear table L is N,l=l1,l2,.., Ln, and the element of the linear table is a finite partial-order set s,| S|=k and K=o (n), S={s1,s2,.. SK}; The count sort can be described as follows:
1, scan the entire set S, for each si∈s, find in the linear table L is less than equal to the number of SI elements T (SI);
2, scan the entire linear table L, Li for each element of L, put Li in the output linear table of the T (LI) position, and T (Li) minus 1.
void Countsort (int* a,int size) {int max = A[0];int min = a[0];for (int i = 1; i<size; i++) {if (A[i] > max) max = A[i];i F (A[i] < min) min = a[i];} int* tem = new Int[max-min +1] (); for (int i =0; i < size; i++) tem[a[i]-Min]++;int Indix = 0;for (int i = 0; i< (max- min + 1); i++) {while (Tem[i] > 0) {a[indix++]= i + min;--tem[i];}} Delete[] tem;}
Base sort
The radix sort (radix sort) belongs to the "distributive sort" (distribution sort), also known as the "bucket method" (bucket sort) or bin sort, as the name implies, it is a part of the information through the key value, the elements to be sorted into some "barrels", In order to achieve the role of sequencing, the cardinal ranking method is a sort of stability, its time complexity is O (Nlog (r) m), where R is the base taken, and M is the number of heaps, at some point, the cardinality of the sorting method is more efficient than other stability ranking method.
In fact, his realization, like sparse implementation of sparse matrices, readers can learn from this, and compared with it.
Int maxbit (int* a, int size) //computes the maximum number of bits in the array. {int bit = 1;int num = 10;for (int i =0; i<size;i++) {if (A[i] > num) {++bit;num *= 10;}} Return bit;} Void radixsort (int* a, int size) {int indix = maxbit (a,size); Int* tem = new int[size]; //Record This value int* count = new int[size]; //Record Count int radix = 1; The value used for the rounding for (int i = 0; i<indix;i++) {for (int j = 0;j < size; j++) {count[j] = 0;} for (int j = 0; j<size; j++) {int k = (A[j]/radix) % 10; count[k]++;} for (int j = 1; j<size; j++) count[j] = count[j-1] +count [j]; for (int j= size-1;j>=0;j--) {INT&NBsp;k = (A[j]/radix) % 10;tem[count[k] -1] = a[j];count[k]--;} for (int j = 0;j < size; j++) {a[j] = tem[j];} radix = radix * 10;} Delete[] tem;delete[] count;}
The above images can be read in the blog post hyperlinks, to give readers the image of understanding and answer
This blog post lists the merge sort, count sort, cardinal sort, basic can master the summary, glimpse, superficial understanding. If you have any suggestions or criticisms and additions, please leave a message stating that you are grateful for more information please visit the Internet.
Common sorting Algorithms (four) (merge sort, count sort, base sort)