Question: Please implement code below to program generate randon arrray instead of fix array. package selectionsort; /** * * @author norinchea */ public class SelectionSort {
Please implement code below to program generate randon arrray instead of fix array.
package selectionsort;
/** * * @author norinchea */ public class SelectionSort { public static void main(String[] args) { int[]numbers ={20,7,15,12,40,12,22,1,5,67}; sort(numbers); printArray(numbers); } public static int[] sort(int[] A){ for(int i = 0; i < A.length-1; i++){ int minIndex = i; for(int j = i+1; j < A.length; j++){ if (A[j] < A[minIndex]){ minIndex = j; } } int temp = A[minIndex]; A[minIndex] = A[i]; A[i] = temp; } return A; }
public static void printArray(int [] A){ for (int i = 0; i < A.length; i++){ System.out.println(A[i]); } } }
--------------------
public class InsertionSort {
public static void main(String[] args) { int []numbers = {10,3,56,7,34,22,1,12,4}; sort(numbers); printArray(numbers); } public static int[] sort(int [] A){ for(int i = 1; i < A.length; i++){ int key = A[i]; int j = i-1; while(j >= 0 && A[j]>key){ A[j+1] = A[j]; j--; } A[j+1] = key; } return A; } public static void printArray (int[] A){ for (int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
