/* * mergesorter.cs-by Chimomo * * Merge sort is a sort algorithm built on merge operations, which is a very typical application of the Divide and conquer strategy (Divide and Conquer). * * The basic principle of the merge operation: * 1, the application space, so that its size is the sum of two sorted sequences, the space used to store the merged sequence. * 2, set two pointers, the initial position is the starting position of two sorted sequence. * 3. Compare the elements pointed to by two pointers, select a relatively small element into the merge sequence, and move the pointer to the next position. * 4, repeat step 3 until a pointer exceeds the tail of the sequence. * 5. Copy all remaining elements of the other sequence directly to the end of the merge sequence. * * Merge the Basic principles of sorting (assuming that the sequence has n elements): * 1, the sequence of each adjacent two number of merge operation (merge), forming floor (N/2) sequence, after ordering each sequence contains two of no element. * 2, the above sequence is merged again, Form floor (N/4) sequence, each sequence contains four elements. * 3, repeat step 2 until all elements are sorted. * * Merge sort speed is second only to fast ordering, which is a stable sort algorithm, which is generally used for the sequence of the whole disorder, but the relative order of each child. */using system;namespace sort{public static class Mergesorter {//<summary>///Two-way merge: Return two ordered sequences And into an ordered sequence. </summary>//<param name= "A" > Pending sequence </param>//<param name= "S1" > starting subscript for the first ordered sequence &L t;/param>//<param name= "S2" > The starting subscript for the second ordered sequence </param>//<param name= "E2" > The end subscript of the second ordered sequence &L t;/param> private static void Merge (int[] A, int s1, int s2, int e2) {int[] tmp = new Int[e2 -S1 +1]; int i = s1, j = s2, k = 0; while (I < S2 && J <= E2) {if (A[i] <= a[j]) { TMP[K] = A[i]; k++; i++; } else {tmp[k] = a[j]; j + +; k++; }} while (I < S2) {Tmp[k] = A[i]; i++; k++; } while (J <= E2) {tmp[k] = a[j]; j + +; k++; } array.copy (tmp, 0, a, S1, TMP.) Length); }///<summary>//merge sort algorithm. </summary>//<param name= "A" > Pending sequence </param>//<param name= "s" > starting subscript for ordered sequence </ param>//<param name= "Length" > The length of the ordered collection to be merged each time </param> public static void MergeSORT (int[] A, int s, int length) {int size = A.length; int mid = size/(length << 1); int c = size & ((length << 1)-1); Merges to the end of the algorithm when there is only one sequential sequence left. if (mid = = 0) {return; }//Make a merge sort. for (int i = 0; i < mid; ++i) {s = i * 2 * length; Merge (A, S, S + length, (length << 1) + s-1); }//Merges the remaining elements with an ordered set of reciprocal. if (c! = 0) {Merge (A, size-c-2 * length, size-c, size-1); }//recursively perform the next merge sort. MergeSort (A, 0, 2 * length); } }}
/* * program.cs-by chimomo **/using system;namespace sort{ Static class program { static void Main () {
int[] A = {4, 3, 6, 1, 2, 5,, 7,-1, 340, -234,-9, 909}; Mergesorter.mergesort (A, 0, 1); foreach (var i in a) { Console.Write (i + ""); } Console.WriteLine ();}}} /*output:-234-9-1 1 2 3 4 5 6 7 34 340 909*/
Algorithm-merge sort (C #)