简体   繁体   中英

Sorting a 2-D Array by Copying it to a 1-D Array, the output is correct but the iterations are repeating

When I sort a 2-D Array which is an argument to a function by Copying it to a 1-D Array, the output is correct but the iterations are repeating. Is my approach correct? I have used an Array "Sort" to copy both the unsorted Arrays & then sort this result set Array.

逻辑和输出的图像

package learningJava;

import java.util.Arrays;

public class VarArgDemoThree 
{

    public static void main(String... args)
    {
        //First one D unsorted Array
        int a[]= {10,2,56,17,81,92};

        //Second one D unsorted Array
        int b[]= {12,77,22,98,101,6};

        //Static method Sort which to which both Arrays a & b are passed
        Sort(a,b);
    }

    //Definition of Static method Sort
    public static void Sort(int[]...x )//This method has a one d array as variable argument which is int[][] it get array (a[],b[])
    {

        //Declaring another one-d Sort of which the length is 12
        int[] Sort = new int[x[0].length+x[1].length];

        //Copying the one D array at location x[0] to another Array Sort using System.arraycopy
        System.arraycopy(x[0], 0, Sort, 0, x[0].length);

        //Copying the one D array at location x[1] to another Array Sort using System.arraycopy
        System.arraycopy(x[1], 0, Sort, x[0].length, x[1].length);

       //Sorting the Elements of the Array Sort 
        for(int i=0;i<Sort.length;i++)
        {
            int flag=0;
            for(int j=i+1;j<Sort.length;j++)
            {
                if(Sort[i]>Sort[j])
                {
                    int temp = Sort[i];
                    Sort[i]  = Sort[j];
                    Sort[j]  = temp;
                    flag=1;
                }
            }

            System.out.print(Arrays.toString(Sort));
            System.out.println();
            if(flag==0)
            {
                break;
            }

        }

    }

}

You are doing that loop more than it is needed.

just see the code below:

for(int i=0;i<Sort.length;i++) // line a
    {
        int flag=0;
        for(int j=i+1;j<Sort.length;j++) // line b
        {
            if(Sort[i]>Sort[j])
            {
                int temp = Sort[i];
                Sort[i]  = Sort[j];
                Sort[j]  = temp;
                flag=1;
            }
        }

        System.out.print(Arrays.toString(Sort)); // line c
        System.out.println();

in "line a" the loop is repeating until i == Sort.length .

in "line b" the loop is starting from j = i + 1 till j == Sort.length .

it is obvious that the last time the inner loop in "line b" does not run but "line c" that is printing Sort will run. that's why you're printing the array one extra time. imagine the inner loop started from Sort.length + 1 till Sort.length .

to avoid that you would better to change the first loop to i < Sort.length - 1 .

some more matters (which are optional):

  • why do you need flag ? you are looping till the end.
  • try to start the name of fields and local variables in lower case. for example use sort instead of Sort .
  • you can use System.out.print("something") and empty System.out.println() together, like System.out.println("something") .
i<Sort.length-1 for the outer loop only works thanks..

    int a[]= {10,6,18,23,97,188,67,45,52};
    int b[]= {4,15,28,77,60,71,90,33,24};    

    for(int i=0;i<Sort.length-1;i++)
                {
                    for(int j=i+1;j<Sort.length;j++)
                    {
                        if(Sort[i]>Sort[j])
                        {[![enter image description here][1]][1]
                            int temp = Sort[i];
                            Sort[i]  = Sort[j];
                            Sort[j]  = temp;
                        }
                    }

                    System.out.println(Arrays.toString(Sort));
                }


  [1]: https://i.stack.imgur.com/5jB1G.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM