Click here to Skip to main content
15,895,784 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<stdio.h>
#include<stdlib.h>
void merge(int left[],int right[],int a[],int half);
void mergesort(int a[],int n);
int main()
{
    int a[6]={3,4,8,1,2,4}; int i;
    mergesort(a,6);
    for(i=0;i<6;i++)
        printf("%d\t",a[i]);

    return 0;
}
void merge(int left[],int right[],int a[],int half)
{
    int i=0,j=0,k=0;
    while(i<half&&j<half)
    {
        if(left[i]<right[j])
        {
          a[k]=left[i];
          i++;
        }
        if(right[j]<left[i])
        {
            a[k]=right[j];
            j++;
        }
    }
    if(i==half)
    {
        for(j=j;j<half;j++)
        {
            a[k]=right[j];
            k++;
        }
    }
    if(j==half)
    {
        for(i=i;i<half;i++)
        {
            a[k]=left[i];
            k++;
        }
    }

void mergesort(int a[],int n)
{
    int half=n/2,i;
    int left[half];
    int right[half];
    for(i=0;i<half;i++)
        left[i]=a[i];
    for(i=0;i<half;i++)
        right[i]=a[i];
    mergesort(left,half);
    mergesort(right,half);
    merge(left,right,a,half);
}
}
Posted
Updated 26-Jul-15 9:08am
v4
Comments
[no name] 26-Jul-15 14:29pm    
You have a misplaced brace at the end. It should be before your mergesort function.

As Wes Aday[^] pointed out it looks like you're missing one closing brace in the merge function and there's one extra after mergesort functtion
 
Share this answer
 
Comments
Member 11324568 30-Jul-15 9:59am    
yes sir, that cleared the error. Thanks. I never faced this error previously so i was confused.
Wendelius 30-Jul-15 10:48am    
Glad to hear it helped.
Are your asking here simply because it is easier than searching by yourself?

Look at documentation, google the error message.

Think I am rude ? This is typical homework to have you practice and learn the language. You can't have somebody to learn programming for you.

Advice
- Learn the language. You will not learn the language by always asking the answer.
- Your language documentation is your friend.
- The error message is your friend. it tells you that your code don't do what you think it is.
- The debugger is your friend. Abuse of debugger.
- Tutorial sites are your friends.
- Google is your friend. You will find there numerous MergeSort snipsets.

I see at least 12 errors and most of them many times.
- the program never end.
- the program loose some data.
- ...
 
Share this answer
 
v7
Comments
Patrice T 26-Jul-15 21:50pm    
To downvoter: This code don't need corrections, it needs a complete rewrite.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900