2-Way Merge sort

Source: Internet
Author: User

#include <iostream>
#include <typeinfo>
#define N 100

using namespace Std;

void mergesort (int *r,int low,int Mid,int high) {
Divide and conquer the law to R[low. High] for two-way merge sort
int *r1=new int[high-low+1];
int i,j,p=0;
i=low;j=mid+1;

if (! R1) {
cout<< "Space allocation failure";
return;}

while (I<=mid&&j<=high) {
R1[p++]= (R[i]<=r[j])? R[i++]:r[j++]; Two sub-files are not empty when they are output to r1[p]
}
while (I<=mid) {//If the 1th sub-file is not empty, copy the remaining records into R1
R1[p++]=r[i++];
}
while (J<=high) {//If the 2nd sub-file is not empty, copy the remaining records into R1
R1[p++]=r[j++];
}
The for (p=0,i=low;i<=high;i++,p++) {//merge results are copied back to R[low after completion: High
R[I]=R1[P];
}
delete [] R1;
}

void Merge (int *r,int low,int high) {
int mid;
Mid= (Low+high)/2;
if (LowMerge (R,low,mid);//recursive sort [low]. Mid
Merge (R,mid+1,high);//recursive sort [Mid+1..high]
MergeSort (R,low,mid,high);//merge left and right into an ordered sequence
}
}

int main ()
{
int N,low,high;
int r[n];
cin>>n;
for (int i=0;i<n;i++)
cin>>r[i];

low=0; high=n-1;
Merge (R,low,high);

for (int i=0;i<n;i++)
cout<<r[i]<< "";

return 0;
}

2-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.