简体   繁体   中英

Sort a list of random arrays from largest to smallest

Alright for my assignment, I have to sort series of random numbers in order from largest to smallest. The random numbers are going from indexOfMaxInRange, take the index of the largest number return that. In swapElements,it basically have to swap the index with the highest element to a[0], second highest to a[1] and so on. These two methods are passed through sortarray. Please give me some input on how to finish this and what I am doing wrong thank you. PS: Not everything are called in the code, for various reason such as printing.

Here is my code:

public static void main(String[] args) {

    int[] array = randomIntArray (10);   
    int index = indexOfMaxInRange (array, -5, 15 );
    swapElement (array, index, 0);

}

  public static int randomInt (int low, int high){ // Create a serie of random numbers
    int x = 0;
    for (int i = 0; i < 10; i++){
        x = (int)(Math.random ()* (high - low) +low);

    }  
    return x;

}
public static int[] randomIntArray (int n) { // Size of array
    int[] a = new int [n];
    for (int i = 0; i <a.length; i++){
        a[i] = randomInt (-5, 15);
    }
    return a;
}

public static int indexOfMaxInRange (int[] a, int low , int high){ //Find the index of the largest element
    int [] b = new int [a.length];
    int index = 0;
    for (int i = 0; i < a.length-1; i++){
        if (a[i] >= low && a[i] < high)index++;  
        if (a[i] > a[i+1]){ 
            b[i] = a[i]; 


        }
        System.out.println (b[i]+"\t"+ (index));
    }  
    return index ;

}
public static int swapElement (int []a, int index , int i){ // Swap the element within the array
    int temp = 0;
    System.out.println ();
    for ( i = 0; i <a.length; i++){
        temp = index;
        a[i] = a[a.length - i - 1]; 
        temp = a[a.length - i - 1]; 


        System.out.println (temp + "\t"+ index);
   } 
    return temp;
}

public static void sortArray (int[] array){ 

    for (int i = 0; i <array.length; i++){    
    int index = indexOfMaxInRange (array, -5, 15 );
    swapElement (array, index, 0);

        System.out.println (); // This will print out the newly arranged order of the numbers.
    }
}
public class SortArray {
    public static void main(String[] args) {
        int[] unsortedArray = getRandomArray(10, -5, 15);
        printArray("Unsorted Array:", unsortedArray);
        System.out.println("Max element is at index: "+getMaxElementIndex(unsortedArray));
        printArray("Sorted Array in descending order:", sortArray("desc", unsortedArray));
        printArray("Sorted Array in asending order:", sortArray("asc", unsortedArray));
    }

    public static int[] getRandomArray(int length, int min, int max) {
        if(length == 0)
            throw new RuntimeException("Cannot create a zero length array.");
        int[] toReturn = new int[length];

        for(int i = 0; i < length; i++) {
            toReturn[i] = getRandomNumber(min, max, toReturn);
        }
        return toReturn;
    }

    public static int getRandomNumber(int min, int max, int[] array) {  
        int toReturn = (int)(Math.random ()* (max - min) +min);
        for(int i = 0; i<array.length;i++)
        {
            //Avoid duplicates and recurse if the number is already found in the array.
            if(toReturn == array[i])
                toReturn = getRandomNumber(min, max, array);
        }
        return toReturn;
    }

    public static void printArray(String label, int[] array) {
        System.out.println(label);
        for(int i=0;i<array.length;i++)
            System.out.print(array[i]+" ");
        System.out.println();
    }

    public static int getMaxElementIndex(int[] array) {
        int maxNum = -9999999;
        int index = -9999999;
        for(int i=0;i<array.length;i++)
        {
            if(maxNum == -9999999 || array[i] >= maxNum) {
                maxNum = array[i];
                index = i;
            }

            if(index == -9999999 && i == array.length - 1)
                index = i;
        }
        return index;
    }

    public static int[] sortArray(String direction, int[] array) {
        int temp = 0;
        if(direction.equalsIgnoreCase("desc") || direction.equalsIgnoreCase("")) {
            for(int i=0; i<array.length - 1; i++) {
                for(int j = i; j < array.length; j++) {
                    if(array[j] >= array[i]) {
                        temp = array[j];
                        array[j] = array[i];
                        array[i] = temp;
                    }
                }
            }
        } else if(direction.equalsIgnoreCase("asc")) {
            for(int i=0; i<array.length - 1; i++) {
                for(int j = i; j < array.length; j++) {
                    if(array[j] <= array[i]) {
                        temp = array[j];
                        array[j] = array[i];
                        array[i] = temp;
                    }
                }
            }
        }
        return array;
    }
}

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