Implementation of two-way merge sorting algorithm-complete C language Program

Source: Internet
Author: User

/*********************************************************************************************** 1. Set two pointers, The initial position is the starting position of the two sorted series 2. Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position 3. Repeat steps 3 until a pointer reaches the end of the sequence 4. Copy all remaining elements of another sequence directly to the merge sequence footer merge sort: Merge sort works as follows (assuming that the sequence has n elements): 1. Merges each contiguous two digits of the sequence to form a floor (N/2) sequence, with each sequence containing two element 2. Merge the above sequence into a sequence of floor (N/4) Each sequence contains four elements 3. Repeat step 2 until all elements are sorted the merge sort is stable, its worst, average, and the best time is O (Nlogn).  However, it requires additional storage space. Why ask? hovertree.com Merge Sort method (merge sort, hereinafter referred to as MS) is a model for the application of the idea of division and administration. The main algorithm operation can be divided into the following steps: Step 1: Divide n elements into two sub-sequences with N/2 elements Step 2: Recursively sort two sub-sequences with MS (finally, the entire original sequence can be decomposed into n subsequence) Step 3: Merge two sorted sequences ************* ***********************************************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<climits>#include<cstdlib>#include<time.h>#include<cstdlib>#include<cstdio>using namespacestd; voidRandom (intA[],intN) {intI=0;      Srand ((unsigned) time (NULL));  while(i<N) {a[i++]=rand (); }  }  voidMergeint*a,intLowintMidintHigh//merge Operations{      intK, Begin1, Begin2, End1, End2; Begin1=Low ; End1=mid; Begin2= Mid +1; End2=High ; int*temp = (int*)malloc((High-Low +1) *sizeof(int));  for(k =0; Begin1 <= end1 && begin2 <= end2; k++)//from childhood to the big sort    {          if(A[begin1] <=a[begin2]) temp[k]= a[begin1++]; ElseTemp[k]= a[begin2++]; }      if(Begin1 <= End1)//left remainingmemcpy (temp + k, A + begin1, (end1-begin1 +1) *sizeof(int)); Else //left Rightmemcpy (temp + k, A + begin2, (end2-begin2 +1) *sizeof(int)); memcpy (A+ Low, temp, (high-low +1) *sizeof(int));//copy to original array after sorting     Free(temp);//Free Space}  voidMerge_sort (int*a, unsignedintBegin, unsignedintend) {      intmid; if(Begin <end) {Mid=begin+ (End-begin) >>1; //mid = (end + begin)/2; Prevent data addition overflowMerge_sort (A, begin, mid);//Divide and conquerMerge_sort (A, Mid +1, end);//Divide and conquerMerge (A, begin, Mid, end);//Merge two sorted columns    }  }  intMain () {inta[ -]; Random (A, -);  for(intI=0;i< -; i++) {cout<<" "<<a[i]<<" "; } merge_sort (A,0, --1);  for(intI=0;i< -; i++) {cout<<" "<<a[i]<<Endl; }        return 0; }  

Recommendation: http://www.cnblogs.com/roucheng/p/cppjy.html

Implementation of two-way merge sorting algorithm-complete C language Program

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.