C language Merge sort (merge sort) algorithm and code

Source: Internet
Author: User

Merge sort is also called merging sort, the algorithm idea is to divide the sequence to be sorted into two parts, then use the merge sort again to divide the two parts, then combine them. Only from the idea of the algorithm to understand the merge sort will feel very abstract, next to the sequence a[0], a[l] ..., a[n-1] in ascending order to explain, in this use the top-down implementation method, the following steps.

(1) The sorting sequence to be carried out into the left and right two parts, if the starting element of the sequence to be ordered to the first, the last element of the next subscript is final, then the right and left between the two parts of the critical point subscript mid= (First+last)/2, the two parts are A[first ... MID] and a[mid+1 ... last].

(2) The two parts of the sequence above are continued to be divided according to step (1) until the interval length of the division is 1.

(3) The sequence after the division is sorted, the sorting method is to divide the N sub-sequence 22 merge, get N/2 or n/2+l contains two elements of the sub-sequence, and then the resulting sub-sequence is merged, until a length of an ordered sequence of N. Here's a piece of code to see how the merge sort is implemented.

#include <stdio.h>#include<stdlib.h>#defineN 7voidMergeintArr[],intLowintMidintHigh ) {    intI, K; int*tmp = (int*)malloc((high-low+1)*sizeof(int)); //request space to make it two size    intLeft_low =Low ; intLeft_high =mid; intRight_low = mid +1; intRight_high =High ;  for(k=0; Left_low<=left_high && right_low<=right_high; k++) {//Compare the elements pointed to by two pointers        if(arr[left_low]<=Arr[right_low]) {Tmp[k]= arr[left_low++]; }Else{Tmp[k]= arr[right_low++]; }    }    if(Left_low <= Left_high) {//if the first sequence has a remainder, it is copied directly to the end of the merge sequence.//memcpy (Tmp+k, Arr+left_low, (left_high-left_low+l) *sizeof (int));     for(i=left_low;i<=left_high;i++) Tmp[k++] =Arr[i]; }    if(Right_low <=Right_high) {    //if the second sequence has a remainder, it is copied directly to the end of the merge sequence.//memcpy (Tmp+k, Arr+right_low, (right_high-right_low+1) *sizeof (int));         for(I=right_low; i<=right_high; i++) Tmp[k++] =Arr[i]; }     for(i=0; i1; i++) Arr[low+i] =Tmp[i];  Free(TMP); return;}voidMerge_sort (intArr[], unsignedintFirst, unsignedintLast ) {    intMID =0; if(first<Last ) {Mid= (first+last)/2;/*Caution against overflow*/        /*mid = First/2 + LAST/2;*/        //mid = (First & last) + ((first ^ last) >> 1);Merge_sort (arr, first, mid); Merge_sort (arr, Mid+1, last);    Merge (Arr,first,mid,last); }    return;}intMain () {inti; inta[n]={ +, A, About, +, the, $, $}; printf ("sort before \ n");  for(i=0; i<n;i++) printf ("%d\t", A[i]); Merge_sort (A,0, N-1);//Sortprintf ("\ n sort after \ n");  for(i=0; i<n;i++) printf ("%d\t", A[i]); printf"\ n"); System ("Pause"); return 0;}

Operation Result:

Sort before the    first    36, after the    order of    78


The result of the above operation is analyzed, and the sorting operation of the given sequence is implemented successfully through the merge sort. The next step is to understand the process of merging sorting by Figure 11-9.

In Figure 11-9, the sequence to be sorted is decomposed until it is divided into a single element, and then 22 is merged. Because it is eventually decomposed into a single element, it is at the time of merging. Place decimals in front and large numbers behind to get an ordered sequence. Next, sort the two contiguous ordered sequences, compare the first element in the ordered sequence, put the smaller elements in the temporary array, and then compare the next element of the array with the smaller element to the smaller elements in the other array, and then place the smaller elements in the temporary array, in turn, Until all elements of the two array are placed in a temporary array, the elements of the temporary array are then placed in the corresponding position in the original array.


Figure 11-9 Operation flow for merge sort

C language Merge sort (merge sort) algorithm and code

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.