简体   繁体   中英

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.

    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. Be sure to change your class name. Next, you'll have to pass the array tab as whole to the sortAsc() method. You need not check for max and min in loop indexed by j as this will tend to make a lot of repetitive comparisons. So move is up to loop indexed by 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. Use static methods only when it's necessary.

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);
    }
}

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