views:

96

answers:

4

hi i have question i know that this question is somehow nonsense but let see i have code to merge two sorted array in a one sorted array here is code in java

public class Merge {

    public static void main(String[]args){

       int a[]=new int[]{7,14,23,30,35,40};
       int b[]=new int[]{5,8,9,11,50,67,81};
       int c[]=new int[a.length+b.length];
       int al=0;
       int bl=0;
       int cl=0;

       while (al<a.length && bl<b.length)              
          if (a[al]<b[bl])
              c[cl++]=a[al++];   
          else  
              c[cl++]=b[bl++];


       while (al<a.length)
          c[cl++]=a[al++];

       while (bl<b.length)
          c[cl++]=b[bl++];

       for (int j=0;j<c.length;j++){
          System.out.println(c[j]);
       }


    }
}

question is why does not work if we write here {} brackets

while (al<a.length && bl<b.length){
}

?

+1  A: 

If you are having trouble with the Merge Sort Algorithm, please check this Wiki Article. Also, when posting code it would be nice to give variables a more descriptive name, as well as adding braces ({ }) to your while and if statements

npinti
+3  A: 

It depends on where you put the brackets, your current code is equivalent to:

while (al<a.length && bl<b.length) {
    if (a[al]<b[bl]) {
       c[cl++]=a[al++];
    } else {
       c[cl++]=b[bl++];
    }
}

If you put the brackets in any other place you will change the semantics of the code.

Andreas Brinck
no i have tried brackets but it does not work
@davit and you inserted your brackets at this position? Or anywhere else?
tanascius
+1  A: 

If my guess is correct, your question is......why the following code doesn't work?

while (al<a.length && bl<b.length)
{ //<-- bracket here

    if (a[al]<b[bl])
      c[cl++]=a[al++];


     else  
     c[cl++]=b[bl++];


    while (al<a.length)
      c[cl++]=a[al++];

    while (bl<b.length)
     c[cl++]=b[bl++];
} //<--- Bracket here

If that is the case,

while (al<a.length && bl<b.length)

would fail after first iteration.

Jujjuru
thanks Jujjuru for help
One iteration to be precise
Andreas Brinck
@Andreas Thanks. Corrected it :)
Jujjuru
+1  A: 

One good advice: always surround all logical blocks of code with curly braces:

if (expression) { 
   //some code 
}

if (expression) {
   //one line of code
} else {
   //another line
}

while (conditionIsTrue) {
   //just 1 line here but surrounded with curly braces 
}

It'll help you avoid numerous not-obvious mistakes in all C-style languages.

How those mistakes can ever appear if you understand that only one line should be in block and you omit braces consciously to make code more readable? Very simple! Just imagine the code:

if (a > b) 
   result = a;

For the first look it's more elegant then

if (a > b) {
   result = a;
}

or even

if (a > b)
{
   result = a;
}

But now let's suppose you want to add some debug info to your code:

if (a > b)
   System.out.println ("a > b, we're inside if-block");
   result = a;                            //This line is ALWAYS executed

As you can see it's really easy to make a mistake. So just don't omit braces and things will be done much easier.

Roman