简体   繁体   English

没有排序方法排序多维数组?

[英]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. 我的任务是创建一个2D数组(10 x 10),用随机数(从10到99)填充它,以及其他任务。 I am, however, having difficulty sorting each row of this array in ascending order without using the array sort() method. 但是,在不使用数组sort()方法的情况下,很难以升序对数组的每一行进行排序。

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; 令x为我们未排序的数组;

        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; //现在x已排序; "-"; “-”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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