Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sortarray;
- public class SortArray {
- public static int randomInt(int low, int high){
- int r = low +(int)(Math.random()*((high - low)+1));
- return r;
- }
- //where i create the array using my method randomInt and min and max from main.
- public static int[] randomIntArray(int min, int max){
- //creates a new array called a, length 30.
- int[] a = new int[10];
- //for loop to store random integers in the array/
- for(int i = 0; i<a.length; i++){
- a[i] = randomInt(min, max);
- }
- return a;
- }
- public static int indexOfMaxInRange(int[] a){
- int maxIdx = 0;
- for(int i = 1; i < a.length;i++){
- if(a[i] > a[maxIdx]){
- maxIdx = i;
- }
- }
- return maxIdx;
- }
- public static int[] swapElement(int[]a){
- for(int i = 0; i < a.length; i++){
- int index = indexOfMaxInRange(a);
- int temp = a[i];
- a[i] = a[index];
- a[index] = temp;
- }
- return a;
- }
- public static void printArray (int[] a) {
- for (int i = 0; i<a.length; i++) {
- System.out.println (a[i]);
- }
- }
- public static void main(String[] args) {
- int min = 0;
- int max = 30;
- int[] a = randomIntArray(min, max);
- int [] b = swapElement(a);
- printArray(b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement