Advertisement
Guest User

sortArray

a guest
Nov 19th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package sortarray;
  2. public class SortArray {
  3.     public static int randomInt(int low, int high){
  4.         int r = low +(int)(Math.random()*((high - low)+1));
  5.         return r;
  6.     }
  7.     //where i create the array using my method randomInt and min and max from main.
  8.     public static int[] randomIntArray(int min, int max){
  9.         //creates a new array called a, length 30.
  10.         int[] a = new int[10];
  11.         //for loop to store random integers in the array/
  12.         for(int i = 0; i<a.length; i++){
  13.             a[i] = randomInt(min, max);
  14.         }
  15.         return a;
  16.     }
  17.     public static int indexOfMaxInRange(int[] a){
  18.         int maxIdx = 0;
  19.         for(int i = 1; i < a.length;i++){
  20.             if(a[i] > a[maxIdx]){
  21.                 maxIdx = i;
  22.             }
  23.         }
  24.         return maxIdx;
  25.     }
  26.     public static int[] swapElement(int[]a){
  27.         for(int i = 0; i < a.length; i++){
  28.             int index = indexOfMaxInRange(a);
  29.             int temp = a[i];
  30.             a[i] = a[index];
  31.             a[index] = temp;
  32.         }
  33.         return a;
  34.     }
  35.     public static void printArray (int[] a) {
  36.         for (int i = 0; i<a.length; i++) {
  37.         System.out.println (a[i]);
  38.         }
  39.     }
  40.     public static void main(String[] args) {
  41.         int min = 0;
  42.         int max = 30;
  43.         int[] a = randomIntArray(min, max);
  44.         int [] b = swapElement(a);
  45.         printArray(b);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement