
Write a method that
takes an integer array as its parameter and sorts the contents of the array in
ascending order using the Insertion Sort
for the algorithm). Call this method (created in exercise 1 of Lab Practice 5)
after the original array and other stats have been displayed. Once the array has
been sorted by your method, display its contents to the screen in the same
manner as the original array was displayed.
public class zxc {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in); //Scanner instance for accepting input from user
System.out.print("Enter the number of random numbers to be generated: ");
int n=sc.nextInt();//input value of n from user
int array[]=new int[n]; //create an array of size n
Random generator=new Random(); //Random instance for generating random numbers
for(int i=0;i<n;i++) //i from 0 to n-1
{
//generate number from 0 to 998 and then add 1 to it
array[i]=1+generator.nextInt(999);
}
int smallest=array[0]; //initialize smallest
int largest=array[0]; //initialize largest
double sum=0; //initialize sum to 0
for(int i=0;i<n;i++) //i from 0 to n-1
{
if(i%5==0) //if i is multiple of 5
System.out.println(); //move to new line
System.out.print(array[i]+" "); //print the number at index i and a tab space
sum=sum+array[i]; //add the element to sum
if(array[i]<smallest) //if element at index i is less than smallest
smallest=array[i]; //update smallest
if(array[i]>largest) //if element at index i is more than largest
largest=array[i]; //update largest
}
System.out.println("\n\nSmallest: "+smallest); //print smallest
System.out.println("Largest: "+largest); //print largest
System.out.println("Average: "+sum/n); //print average
}
}

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 3 images

- Define an empty array with 4 elements inside the "main" method. The value '-1' from the user to this arrayRecord the numbers entered by the user in order until they arrive. User array with 4 elementsAfter adding, write the required method so that it can continue adding elements. This method will take the array as a parameter, expand the size of the array and return the array variable.will return. Using this method you defined, the user can add elements to the array.make it continue.arrow_forwardA class Date has methods intgetMonth() and intgetDay(). Write a method public boolean has3OnSameDay(Date[] birthdays) that returns true if at least three birthdays in the array fall on the same date. Your method should work in O(n) time, where n = birthdays.length.arrow_forwardWrite a Java program with a single-dimension array that holds 10 integer numbers and identify the maximum value of the 10 numbers. Next sort the array using a bubble sort and display the array before and after sorting.arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





