Two-way merge sort

Source: Internet
Author: User

The two-way merge sort mainly uses the "divide-and-conquer" algorithm, which divides a large problem into n small and structurally similar sub-problems.

These sub-problems are solved in a similar way, after solving these small problems, the result of merging sub-problems, we get the solution of "big" problem.

Two-way merge sort main idea is "decomposition" and "merge"

Decomposition:

1. Divide an array into two arrays and sort the two arrays separately.

2. Loop the first step until the divided "decimal group" contains only one element, and the array of only one element is already sorted by default.

Merge:

1. Merge two ordered arrays into one large array.

2. Start 22 merge from the smallest array containing only one element. In this case, the merged arrays are also ordered.

            Figure 1. Merge sort process Figure 2. Merging two ordered arrays

To illustrate:

1. The original array in the figure is {2,4,7,5,8,1,3,6}, and the number of elements in the array is 8. First, the array of 8 elements is divided into two, after each decomposition,

The number of elements in the array is the general of the original array. Until it is decomposed into an array that contains only one element.

2. The small array is merged sequentially, and the size of the array after each merge is one of the times of the upper array. The elements in the array are ordered sequentially.

3. Merge two ordered arrays. 2

(1) When merging, there are two pointers to the starting element of the ordered array A and B, comparing the size of the element referred to by the pointer, and if A[I] is smaller, then a[i]

In array c, and I will move back. The cycle compares the elements that I and J refer to.

(2) When the elements of an array A are all lined up, the pointer in array B does not point to the end of B, and all the remaining elements in B are stored in C.

1#include <stdio.h>2#include <stdlib.h>3 4 voidMerge (intArray[],intPintQintR);5 voidMergeSort (intArray[],intPintq);6 7 intMain ()8 {9     intarray[8] = {5,2,4,7,1,3,2,6};Ten     inti =0; One  AMergeSort (Array,0,7); -     //Merge (Array, 0, 2, 6); -  the      for(I; I <8; i++) -printf"%d", Array[i]); -     return 0; - } +  - //P<=q<r During the merge process + voidMerge (intArray[],intPintQintR) A { at     intN1 = Q-p +1; -     intN2 = R-Q; -  -     int*L; -L = (int*) malloc (sizeof(int)*N1); -     int*R; inR = (int*) malloc (sizeof(int)*n2); -  to     inti =0; +     intj =0; -  the      for(i; I < N1; i++) *L[i] = array[i +p]; $      for(J; J < N2; J + +)Panax NotoginsengR[J] = array[j + q +1]; -  thei = j =0; +  A     intK =p; the  +      while(I!=n1 && j!=n2) -     { $         if(L[i] <=R[j]) $array[k++] = l[i++]; -         Else -array[k++] = r[j++]; the     } - Wuyi      while(I <N1) thearray[k++] = l[i++]; -      while(J <n2) Wuarray[k++] = r[j++]; - } About  $ voidMergeSort (intArray[],intPintq) - { -     if(P <q) -     { A         intR = (p+q)/2; + mergesort (Array, p, r); theMergeSort (Array, r+1, q); - Merge (array,p, R, Q); $     } the}

Reference: Introduction to the algorithm 2.1 chapters

If there is any problem, please leave me a message, I will correct, we progress together. Thank you ~

  

Two-way 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.