-2
class Temp
{

    static void sort1(int x[][])           //sort 2d  array
    {
        int temp = 0;
        for(int i=0;i<x.length;i++)
        {
            for(int j=i;j<x[i].length;j++)
            {
                for(int k=i;k<x.length;k++)
                {
                    for(int l=j;l<x[k].length;l++)
                    {
                        if(x[i][j] > x[k][l])
                        {
                            temp = x[i][j];
                            x[i][j] = x[k][l];
                            x[k][l] = temp;
                        }                        
                    }
                }
            }
        }
        System.out.println(".....Sorted Array.....");
        for(int i=0;i<x.length;i++)
        {
            for(int j=i;j<x[i].length;j++)
            {
                System.out.print(x[i][j] + "  ");
            }
        }
    }


    public static void main(String... s)
    {
        sort1(new int[][] { 
            {12, 7, 65}, 
            {87, 1, 4, 5, 31}, 
            {9, 76} 
            });
    }
}

When I'm running this program it is giving the output as

1 4 5 7 12 31 65.

I expected it to be

1 4 5 7 9 12 31 65 76 87.

What is wrong with my code? It's missing results that it should be showing. Any help or insight would be greatly appreciated.

  • 1
    What happened when you debugged the code? – Kon Aug 11 '17 at 18:49
  • It's giving unexpected results every time. I changed elements of the array that I'm passing as an argument. It even not printing all the elements. – Rahul Malkani Aug 11 '17 at 19:01
  • 1
    At which specific line number, while debugging, did you observe that the results were unexpected based on what you expected to see? – Kon Aug 11 '17 at 19:03
  • I think something is wrong with loops! – Rahul Malkani Aug 11 '17 at 19:05
  • 1
    Don't think we're understanding each other. Start here: http://www.vogella.com/tutorials/EclipseDebugging/article.html If you don't use Eclipse, search `[IDE NAME] debugging` in Google – Kon Aug 11 '17 at 19:06
  • 1
    [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Aug 11 '17 at 19:10
  • I'm not using any IDE. I compile them on cmd. – Rahul Malkani Aug 11 '17 at 19:13
  • 1
    You have two choices: Get an IDE with a built in debugger (they're all mostly free), or use a pen and paper. Harder, but definitely still doable. – Kon Aug 11 '17 at 19:16

1 Answers1

-2

Here are the bugs I could spot:

  1. When you output the result, in the inner loop you need to start with 0 and not i as you want to process all the elements of the array and not only its half under the main diagonal

    for(int i=0;i<x.length;i++)
    {
        for(int j=0;j<x[i].length;j++)
        {
            System.out.print(x[i][j] + "  ");
        }
    }
    
  2. The same thing with the second loop in your sorting phase: it must start with 0, the reason is the same

    for(int j=0;j<x[i].length;j++)

  3. There is a problem with sorting. In one-dimentional case, you'd have two loops, and the inner one would start with i (where i is the index of the outer loop). In your 2-dimentional case, two outer loops stand for the only outer loop of 1-dimensional case, and two inner loops stand for the only inner loop of 1-dimensional case. Your 2 inner loops need to iterate over all the remaining elements of row i till the end (columns j to the last), plus all the rest rows (rows from i+1 to the last, indices from 0 to the last). But instead you iterate over elements from j to last in all the remaining rows, so you miss some elements (which reside in columns between 0 and j-1 in rows i to the last one).
Roman Puchkovskiy
  • 11,415
  • 5
  • 36
  • 72