C++ Programming

/* Name: Merge Sort
** Author:
** C++ Programming Assignment
*/
#include <iostream>
using namespace std;

class MergeSort{

int max(int x, int y)
{
if(x > y){
return x;
}
else {
return y;
}
}

void merge_assist(int *input, int left, int right, int *initial)
{
if(right == left + 1)
{
return;
}
else
{
int i = 0;
int length = right – left;
int midpoint = length/2;
int l = left, r = left + midpoint;

merge_assist(input, left, left + midpoint, initial);
merge_assist(input, left + midpoint, right, initial);

for(i = 0; i < length; i++)
{

if(l < left + midpoint &&
(r == right || max(input[l], input[r]) == input[l]))
{
initial[i] = input[l];
l++;
}
else
{
initial[i] = input[r];
r++;
}
}
for(i = left; i < right; i++)
{
input[i] = initial[i – left];
}
}
}

int mergesort(int *input, int size)
{
int *initial = (int *)malloc(size * sizeof(int));
if(initial != NULL)
{
merge_assist(input, 0, size, initial);
free(initial);
return 1;
}
else
{
return 0;
}
}

int main()
{

mergesort( );

return 0;

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: