Merge sort and specific introduction

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>//complexity analysis//t (n) = t (N/2) + t (N/2) + O (n) t (n) = O (NLOGN)//Merging two sequences         L = left start position, R = right start position rightend = right end position void Merge (int a[],int tmpa[],int l,int r,int rightend) {int leftend = R-1;                Left end position right and left two columns next to int Tmp = L;       Storage result initial position int numelements = rightend-l + 1;        Total number of elements while (L <= leftend && r<= rightend)//when there are elements on both left and right, the comparison size will be small to save in the array tmp {if (A[l] <a[r]) If the value on the left is small then the left element is stored in the array tmpa[tmp++] = a[l++];else//Converse tmpa[tmp++] = a[r++];}  while (L <= leftend)//If the left-hand array is a bit longer then directly into the array tmpa[tmp++] = A[l++];while (R <= rightend) tmpa[tmp++] = a[r++]; Conversely for (int i = 0; i < numelements;i++,rightend-)//NumElem to control the number of assignments a[rightend] = Tmpa[rightend];}                  The merge sort is recursively then returned to sort L = initial position rightend = end position void msort (int a[],int tmpa[],int l,int rightend) {int Center;     Used to store intermediate positions if (L < rightend) {Center = (L + rightend)/2; Calculate the median position msort (a,tmpa,l,center);         Recursive left Msort (a,tmpa,center+1,rightend);       Recursive right-hand merge (A,tmpa,l,center+1,rightend);                       The recursive completion of the program to merge}}//Unified function pretext to reduce the number of parameters void Merge_sort (int a[],int N) {//int *tmpa; Dynamically allocates a large array such as an array of a and a tmpa//tmpa = (int *) malloc (sizeof (A)); int tmpa[100]; Msort (a,tmpa,0,n-1);}                      int main () {int a[11] = {1,3,5,7,9,2,4,6,8,10,20};int B[11] = {0};                    From when the temp array//merge (a,b,0,5,10); Merged Test Msort (a,b,0,10), for (int i=0;i<11;i++) {printf ("%d", a[i]), if (i==10) printf ("\ n");} int c[11] = {1,9,5,2,3,4,6,7,8,5,2}; Msort (c,b,0,10), for (int i=0;i<11;i++) {printf ("%d", c[i]), if (i==10) printf ("\ n");} int d[5] = {3,8,6,5,2}; Merge_sort (d,5), for (int i=0;i<5;i++) {printf ("%d", d[i]), if (i==4) printf ("\ n");} System ("pause"); return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Merge sort and specific introduction

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.