C # Merge sort
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace sort {class Mergesorter {//<summary>//merge sorted by: Merge sort entry//< /summary>//<param name= "Data" > Unordered array </param>//<returns> ordered array </returns> public static int[] Sort (int[] data) {//If data is null, or only 1 or 0 elements are left, return, do not sort if (nul L = = Data | | Data. Length <= 1) {return data; }//take array intermediate subscript int middle = data. Length >> 1; Initializes a temporary array let,right and defines result as the final ordered array, and if the array element is odd, the extra element space is reserved in the right temporary array int[] left = new Int[middle]; Int[] right = new int[data. Length-middle]; Int[] result = new Int[data. Length]; for (int i = 0; i < data. Length; i++) {if (I < middle) { Left[i] = Data[i]; } else {Right[i-middle] = data[i];//I-middle here, let me omit the definition of a J, improved performance }}} = sort (left);//recursive right-hand-array---= sort result = Merge (left, right);//start sorting return result; }///<summary>//merge Sort by: Sort in this step///</summary>//<param name= "a" ; left array </param>///<param name= "B" > Right array </param>//<returns> merge left-and-right array to return </return s> private static int[] Merge (int[] A, int[] b) {//define an array of results to store the final result int[] result = new Int[a.length + b.length]; int i = 0, j = 0, k = 0; while (I < a.length && J < b.length) {if (A[i] < B[J])//The element in the left array is less than the element in the right array {result[k++] = a[i++];//the smallThat put to the result array} else//the element in the left array is greater than the element in the right array {result[k++] = b[ j++];//put the small one in the result array} while (I < a.length)//Here is actually the left element, but there is no right element {result[k++] = a[i++]; } while (J < b.length)//have right element, no left element {result[k++] = b[j++]; } return result;//return the result array}}}
Merge sort:
Merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, the ordered sequence is divided into several sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence. This algorithm is a very typical application of the partition 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 you combine two ordered tables into an ordered table, it is called a 2-way merge.
Assuming we have a sequence that is not well sequenced, first we use the segmentation method to divide the sequence into a sequenced sequence, and then merge the sub-sequences into sorted sequences by merging them. The process of splitting and merging can be seen in the following illustration.
As can be seen, we first put an unordered sequence from the middle into 2 parts, and then divide the 2 parts into 4 parts, then split down, until divided into a single data, and then the data 22 merged together, so that the order, keep merging, and finally become a sequential sequence.
How do you merge two sorted sub-sequences into a sequential sequence? You can see the following methods.
Let's say we have two sub-sequences that are already sorted.
Sequence A:1 23 34 65
Sequence B:2 13 14 87
Then you can follow the steps below to merge them into a sequence.
(1) First set a new sequence c[8].
(2) a[0] and b[0] comparison, a[0] = 1,b[0] = 2,a[0] < b[0], then c[0] = 1
(3) a[1] and b[0] comparison, a[1] = 23,b[0] = 2,a[1] > b[0], then c[1] = 2
(4) a[1] and b[1] comparison, a[1] = 23,b[1] = 13,a[1] > b[1], then c[2] = 13
(5) A[1] and b[2] comparison, a[1] = 23,b[2] = 14,a[1] > b[2], then c[3] = 14
(6) A[1] and b[3] comparison, a[1] = 23,b[3] = 87,a[1] < b[3], then c[4] = 23
(7) a[2] and b[3] comparison, a[2] = 34,b[3] = 87,a[2] < b[3], then c[5] = 34
(8) A[3] and b[3] comparison, a[3] = 65,b[3] = 87,a[3] < b[3], then c[6] = 65
(9) finally copy b[3] to C, then c[7] = 87. Merge complete.
C # Shift Operations (shift left and right)
Merge sort, time complexity is O (NLOGN).
The efficiency of the merge sort is relatively high, set the sequence length is N, divide the series into small series to Logn step, each step is a process of merging ordered series, the time complexity can be recorded as O (N), so altogether O (N*logn). Because the merge sort is done in the adjacent data each time, several sorting methods (quick sorting, merge sort, hill sort, heap sort) of the merge sort in O (N*logn) are also relatively efficient.
The above is the C # merge sort of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!