Question: I have a program that creates an array of 10 random numbers. Then the array must be sorted by insertion and selection. So my question
I have a program that creates an array of 10 random numbers. Then the array must be sorted by insertion and selection. So my question is, how can I display the values of the array before I sort, after each intermediate step and when it's sorted for both sorting types insertion and selection?
I have some code already but it just shows null in output. Here is my code and output below:
import java.util.Arrays;
public class client {
public static void main(String[] args) { Integer[] array = new Integer[10]; //To print the selection sorted array for (int i = 0; i < array.length; i++) array[i] = (int)(Math.random() * 100); ArraySorter.insertionSort(array, array.length); System.out.println(Arrays.toString(array)); selctionSort(); insertionSort (); //PART 2 System.out.println("===================================================================================="); }//end main
//method for selection sorting public static void selctionSort () { Integer[] array = new Integer[10]; for (int i = 0; i < array.length; i++) { System.out.println(Arrays.toString(array)); }// end for
System.out.println(); }//end selctionSort
//method for insertion sorting public static void insertionSort () { Integer[] array = new Integer[10]; for (int i = 0; i < array.length; i++) { System.out.println(Arrays.toString(array)); }// end for
System.out.println(); }//end insertionSort }//end client
OUTPUT FOR CODE:
[1, 5, 11, 21, 36, 40, 54, 57, 70, 88] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null]
[null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null] [null, null, null, null, null, null, null, null, null, null]
====================================================================================
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
