简体   繁体   中英

Sorting multidimensional array without sort method?

I was tasked with creating a 2D array (10-by-10), filling it with random numbers (from 10 to 99), and other tasks. I am, however, having difficulty sorting each row of this array in ascending order without using the array sort() method.

My sorting method does not sort. Instead, it prints out values diagonally, from the top leftmost corner to the bottom right corner. What should I do to sort the numbers?

Here is my code:

public class Program3 
{
    public static void main(String args[])
    {
        int[][] arrayOne = new int[10][10];
        int[][] arrayTwo = new int[10][10];
        arrayTwo = fillArray(arrayOne);

        System.out.println("");

        looper(arrayTwo);

        System.out.println("");

        sorter(arrayTwo);


    }
    public static int randomRange(int min, int max)
    {
        // Where (int)(Math.random() * ((upperbound - lowerbound) + 1) + lowerbound);
        return (int)(Math.random()* ((max - min) + 1) + min);
    }
    public static int[][] fillArray(int x[][])
    {
        for (int row = 0; row < x.length; row++)
        {
            for (int column = 0; column < x[row].length; column++)
            {
                x[row][column] = randomRange(10,99);
                System.out.print(x[row][column] + "\t");
            }
            System.out.println();
        }
        return x;
    }
    public static void looper(int y[][])
    {
        for (int row = 0; row < y.length; row++)
        {
            for (int column = 0; column < y[row].length; column++)
            {
                if (y[row][column]%2 == 0)
                {
                    y[row][column] = 2 * y[row][column];

                    if (y[row][column]%10 == 0)
                    {
                        y[row][column] = y[row][column]/10;

                    }
                }
                else if (y[row][column] == 59)
                {
                    y[row][column] = 99;
                }
                System.out.print(y[row][column] + "\t");
            }
            System.out.println();
        }
        //return y;
    }
    public static void sorter(int[][] z)
    {
        int temp = 0;
        int tempTwo = 0;
        int lowest; 
        int bravo = 0;
        int bravoBefore = -1;

        for (int alpha = 0; alpha < z.length; alpha++)
        {
            //System.out.println(alpha + "a");

            lowest = z[alpha][bravoBefore + 1];
            bravoBefore++;
            for (bravo = alpha + 1; bravo < z[alpha].length; bravo++)
            {
                //System.out.println(alpha + "b");
                temp = bravo;

                if((z[alpha][bravo]) < lowest)
                {

                    temp = bravo;
                    lowest = z[alpha][bravo];
                    //System.out.println(lowest + " " + temp);
                    //System.out.println(alpha + "c" + temp);
                    tempTwo = z[alpha][bravo];
                    z[alpha][bravo] = z[alpha][temp];
                    z[alpha][temp] = tempTwo;   
                    //System.out.println(alpha + "d" + temp);
                }

            }

            System.out.print(z[alpha][bravoBefore] + "\t");
        }
        /*
        for (int alpha = 0; alpha < z.length; alpha++)
        {
            for (int bravo = 0; bravo < z.length - 1; bravo++)
            {
                if(Integer.valueOf(z[alpha][bravo]) < Integer.valueOf(z[alpha - 1][bravo]))
                {
                    int[][] temp = z[alpha - 1][bravo];
                    z[alpha-1][bravo] = z[alpha][bravo];
                    z[alpha][bravo] = temp;
                }
            }
        }
         */
    }
}
for(int k = 0; k < arr.length; k++)
        {
            for(int p = 0; p < arr[k].length; p++)
            {
                least = arr[k][p];
                for(int i = k; i < arr.length; i++)
                {
                    if(i == k)
                        z = p + 1;
                    else
                        z = 0;
                    for(;z < arr[i].length; z++)
                    {
                        if(arr[i][z] <= small)
                        {
                            least = array[i][z];
                            row = i;
                            col = z;
                        }
                    }
                }
            arr[row][col] = arr[k][p];
            arr[k][p] = least;
            System.out.print(arr[k][p] + " ");
            }
            System.out.println();
        }

Hope this code helps . Happy coding

let x is our unsorted array;

        int t1=0;
        int i1=0;
        int j1=0;
        int n=0;

        boolean f1=false;

        for(int i=0;i<x.length;i++){

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

                t1=x[i][j];



                for(int m=i;m<x.length;m++){


                    if(m==i)n=j+1;  

                    else n=0;   

                    for(;n<x[m].length;n++){

                        if(x[m][n]<=t1){

                            t1=x[m][n];
                            i1=m;
                            j1=n;
                            f1=true;
                        }                       

                    }


                }

                if(f1){ 
                    x[i1][j1]=x[i][j];
                    x[i][j]=t1;
                    f1=false;
                }
            }   


        }

//now x is sorted; "-";

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