简体   繁体   English

使用 java 中的子程序对数组进行升序排序

[英]Sort an array in ascending order by using a subroutine in java

I need to create a subroutine that sorts the array from ascending order and i want to display under the old array the new sorted one along with the maximum value in it and the minimum value in it.我需要创建一个从升序对数组进行排序的子例程,并且我想在旧数组下显示新排序的数组以及其中的最大值和最小值。

so far i only came up with this code and it has a lot of errors in it到目前为止,我只提出了这段代码,其中有很多错误

PS i don't want to use the array.sort() function. PS 我不想使用 array.sort() function。

    public class acsendingarrays {

        public static void main(String[] args) {
            int i;
            int[] tab=new int[30];
            for(i=0;i<tab.length;i++)tab[i]=(int)(Math.random()*1001);
            for(i=0;i<tab.length;i++)System.out.print(tab[i]+" ");

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

             for (int j = i; j < tab.length; j++) {
                 sortAsc(int[i])


             }

             }
        }

        static int sortAsc(int tab[]) {
            int temp;
            int max = 0, min = 1000;
            int i;

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

                for (int j = i; j < tab.length; j++) {
                    if (tab[i] > tab[j]) {
                        temp = tab[i];
                        tab[i] = tab[j];
                        tab[j] = temp;
                    }
                    if (tab[i] > max) {
                        max = tab[i];
                    }
                    if (tab[i] < min) {
                        min = tab[i];
                    }
                }

            }
            return tab[i];
        }
    }

i dont know why you need those max and min vars for, but try out this code:我不知道你为什么需要这些最大和最小变量,但试试这个代码:

public class acsendingarrays {
    public static void main(String[] args) {
    int i;
    int[] tab=new int[30];
    for(i=0;i<tab.length;i++)tab[i]=(int)(Math.random()*1001);
    for(i=0;i<tab.length;i++)System.out.print(tab[i]+" ");
    System.out.println("");
    sortAsc(tab);
    for(i=0; i<tab.length; i++)System.out.print(tab[i] + " ");
}




static void sortAsc(int tab[]) { 
int temp;
int max=0 , min = 1000;
int i;

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

 for (int j = i; j < tab.length; j++) { 
     if (tab[i] > tab[j]) {
         temp = tab[i];
         tab[i] = tab[j];
         tab[j] = temp;
     }
     if (tab[i] > max) { 
        max = tab[i];
     }
     if (tab[i] < min) {
        min = tab[i];
     }
 }


}
}

} }

The first mistake you've made is the class name.您犯的第一个错误是 class 名称。 Be sure to change your class name.请务必更改您的 class 名称。 Next, you'll have to pass the array tab as whole to the sortAsc() method.接下来,您必须将整个数组选项卡传递给 sortAsc() 方法。 You need not check for max and min in loop indexed by j as this will tend to make a lot of repetitive comparisons.您无需检查由 j 索引的循环中的最大值和最小值,因为这往往会进行大量重复比较。 So move is up to loop indexed by i.所以移动取决于由 i 索引的循环。 If you're a beginner it's okay.如果你是初学者,没关系。 But if you're in college, it's highly recommended you learn the sorting methods and start using sort() method.但是如果你在大学,强烈建议你学习排序方法并开始使用 sort() 方法。 Use static methods only when it's necessary.仅在必要时使用 static 方法。

import java.lang.Math;
import java.util.Arrays;

public class AscendingArrays {
    public static void main(String[] args) {
        int tab[] = new int[30];

        for(int i = 0; i < tab.length; i++) 
            tab[i] = (int)(Math.random()*1001);

        System.out.println(Arrays.toString(tab));

        AscendingArrays a = new AscendingArrays();
        a.sortAsc(tab);

        System.out.println(Arrays.toString(tab));
    }
    public void sortAsc(int[] tab) {
        int max = 0, min = 10000;
        for(int i = 0; i < tab.length; i++) {
            for(int j = i + 1; j < tab.length; j++) {
                if(tab[i] > tab[j]) {
                    tab[i] ^= tab[j];
                    tab[j] ^= tab[i];
                    tab[i] ^= tab[j];
                }

            }
            if(max < tab[i]) max = tab[i];
            if(min > tab[i]) min = tab[i];
        }
        System.out.println("Largest :" + max);
        System.out.println("Smallest:" + min);
    }
}

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

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