Sorting and finding (5)-merge sort

Source: Internet
Author: User

Merge sort is a divide-and-conquer algorithm. 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 ordered sub-sequences, and then the ordered sub-sequence is merged into a whole ordered sequence.  The Merg () function is used to merge two ordered arrays. is the key to the entire algorithm. Look at the description of the MergeSort function in the following description:

mergesort (arr[], L,  > l     1. Find the middle point and say arr is divided into               two parts:= (l+r)/2     2. Call                mergesort (arr, L, m)     for part of MergeSort:3. Call MergeSort for the second part: Calls             mergesort (arr, M+1, R     )4. Merge these two parts: Call             merge (arr, L, M, R)

From Wikipedia, shows the complete merge sort process. For example, the array {38, 27, 43, 3, 9, 82, 10}.

The following is the implementation of the C program:

1#include <stdlib.h>2#include <stdio.h>3 4 /*Merge the left and right parts of arr: Arr[l. M] and ARR[M+1..R]*/5 voidMergeintArr[],intLintMintR)6 {7     intI, J, K;8     intN1 = M-l +1;9     intN2 = R-m;Ten  One     /*Create temp Arrays*/ A     intl[n1], r[n2]; -  -     /*copy data to l[] and r[]*/ the      for(i =0; I < N1; i++) -L[i] = arr[l +i]; -      for(j =0; J <= N2; J + +) -R[J] = arr[m +1+j]; +  -     /*merge the two parts into the arr[l. R]*/ +i =0; Aj =0; atK =l; -      while(I < N1 && J <n2) -     { -         if(L[i] <=R[j]) -         { -ARR[K] =L[i]; ini++; -         } to         Else +         { -ARR[K] =R[j]; theJ + +; *         } $k++;Panax Notoginseng     } -  the     /*Copy the rest of the section l[]*/ +      while(I <N1) A     { theARR[K] =L[i]; +i++; -k++; $     } $  -     /*Copy the rest of the section r[]*/ -      while(J <n2) the     { -ARR[K] =R[j];WuyiJ + +; thek++; -     } Wu } -  About /*sort data arr, from L to R*/ $ voidMergeSort (intArr[],intLintR) - { -     if(L <R) -     { A         intm = L + (r-l)/2;//and (L+r)/2, but can avoid overflow in L and r large + mergesort (arr, L, m); theMergeSort (arr, m+1, R); - merge (arr, L, M, R); $     } the } the  the voidPrintArray (intA[],intsize) the { -     inti; in      for(i=0; i < size; i++) theprintf"%d", A[i]); theprintf"\ n"); About } the  the /*Test Program*/ the intMain () + { -     intArr[] = { A, One, -,5,6,7}; the     intArr_size =sizeof(arr)/sizeof(arr[0]);Bayi  theprintf"Given array is \ n"); the PrintArray (arr, arr_size); -  -MergeSort (arr,0, Arr_size-1); the  theprintf"\nsorted array is \ n"); the PrintArray (arr, arr_size); the     return 0; -}

Time complexity: O (NLOGN) spatial complexity: O (n) stable sequencing

Application of merge Sort:

1) sort the linked list. Other sorting algorithms such as heap sorting and quick sort cannot sort the list. Reference: Sorting a linked list using merge sort

2) calculates the inverse pairs in an array. Sword Point Offer (09)-Reverse order in the array [Divide and conquer]

3) out of order.

Sorting and finding (5)-merge sort

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.