Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i was trying to implement merge sort and tried doing it with a very rudimentary and crude method. As far as i see , i dont seem to find a problem with my logic nut the output screen after taking the input value of array just crashes.
I would be highly grateful if someone can suggest a better way or find error in my approach.

What I have tried:

#include<iostream

int *merge(int *p,int n);
int *halfa(int *ptr,int n);
int *halfb(int *ptr,int n);
int main()
{    int *a,arr[8];
    cout<<"enter";
    for(int i=0;i<8;i++) 
    {
               cin>>arr[i];   }

a=merge(arr,8);

cout<<"\n";

for(int i=0;i<8;i++,a++)

        { cout<<*a;}

return 0;
}

int *merge(int *p,int n)
{ 
 int *q,*r,*t;
 q=halfa(p,16);
 r=halfb(p,16);
 for(int i=0;i<16;i++,t++)
   {
         if(*q<*r)
            {*t=*q;
                      q++;}
        
         if(*r<*q)
             {*t=*r;
                       r++;}
}
return(t);
}

int *halfa(int *ptr,int n)
{         int *q,*y;
           
           for(int i=0;i<(n/2);i++,q++,ptr++)
             {
                   q=ptr;
                            }

y=merge(q,(n/2));

return(y);
}
int *halfb(int *ptr,int n)
   {  int *q,*y;
       for(int i=0;i<n;i++,q++,ptr++)
        {
                if(i>=(n/2))
                 { q=ptr;
                    break; }
                                         }

  y=merge(q,(n/2));
    return(y);
 }
Posted
Updated 27-Dec-17 10:07am
Comments
Peter_in_2780 27-Dec-17 16:10pm    
".. suggest a better way.." It's called the DEBUGGER. Step through your code, watch the variables and control flow.
Rick York 27-Dec-17 16:31pm    
Your code has a number of literal values sprinkled through it and that is really bad idea. I am referring to the 8s and 16s in there. What happens if you decide to have 9 or 10 values? You have a whole bunch of places to change it and then hope you got them all. It is better to use a constant value to do this like :
const int InputCount = 8; and replace all instances of 8 with InputCount. Then if you decide to have a different number of inputs later on you will have just one thing to change. If the 16s are dependent on the other value being 8 then those should be changed to 2*InputCount. Again - just one thing to change if you have to. It may not seem like it now but this can prove to be very important in the future.
Member 13596568 27-Dec-17 17:37pm    
Thanks for your input.
I actually was trying to make it work fro 16 at least and then use the const value.
But no issues there are alot of bugs in the above code which i found after reviewing it again.
Thanks a lot again

1 solution

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include<iostream>

int *merge(int *p,int n);
int *halfa(int *ptr,int n);
int *halfb(int *ptr,int n);
int main()
{
  int *a,arr[8];
  cout<<"enter";
  for(int i=0;i<8;i++)
  {
    cin>>arr[i];
  }

  a=merge(arr,8);

  cout<<"\n";

  for(int i=0;i<8;i++,a++)

  {
    cout<<*a;
  }

  return 0;
}

int *merge(int *p,int n)
{
  int *q,*r,*t;
  q=halfa(p,16);
  r=halfb(p,16);
  for(int i=0;i<16;i++,t++)
  {
    if(*q<*r)
    {
      *t=*q;
      q++;
    }

    if(*r<*q)
    {
      *t=*r;
      r++;
    }
  }
  return(t);
}

int *halfa(int *ptr,int n)
{         int *q,*y;

  for(int i=0;i<(n/2);i++,q++,ptr++)
  {
    q=ptr;
  }

  y=merge(q,(n/2));

  return(y);
}
int *halfb(int *ptr,int n)
{  int *q,*y;
  for(int i=0;i<n;i++,q++,ptr++)
  {
    if(i>=(n/2))
    {
      q=ptr;
      break;
    }
  }

  y=merge(q,(n/2));
  return(y);
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
Why do you have the constant 16 in your code?
You merge in t, but where is it pointing to?

Advice: learn the debugger to see what your code is doing, it is also a great learning tool.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 13596568 27-Dec-17 17:34pm    
Thanks a lot for your review.
I think i posted this question very hurriedly without bothering to review it again and on reviewing i actually found many bugs in this.
Thnaks a lot again

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