java - Sort a list of random arrays from largest to smallest -


alright assignment, have sort series of random numbers in order largest smallest. random numbers going indexofmaxinrange, take index of largest number return that. in swapelements,it have swap index highest element a[0], second highest a[1] , on. these 2 methods passed through sortarray. please give me input on how finish , doing wrong thank you. ps: not called in code, various reason such printing.

here 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 serie of random numbers     int x = 0;     (int = 0; < 10; i++){         x = (int)(math.random ()* (high - low) +low);      }       return x;  } public static int[] randomintarray (int n) { // size of array     int[] = new int [n];     (int = 0; <a.length; i++){         a[i] = randomint (-5, 15);     }     return a; }  public static int indexofmaxinrange (int[] a, int low , int high){ //find index of largest element     int [] b = new int [a.length];     int index = 0;     (int = 0; < 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 element within array     int temp = 0;     system.out.println ();     ( = 0; <a.length; i++){         temp = index;         a[i] = a[a.length - - 1];          temp = a[a.length - - 1];            system.out.println (temp + "\t"+ index);    }      return temp; }  public static void sortarray (int[] array){       (int = 0; <array.length; i++){         int index = indexofmaxinrange (array, -5, 15 );     swapelement (array, index, 0);          system.out.println (); // print out newly arranged order of 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 @ 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 0 length array.");         int[] toreturn = new int[length];          for(int = 0; < 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 = 0; i<array.length;i++)         {             //avoid duplicates , recurse if number found in 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 && == 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;     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -