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 algorithm (see Topic 6 lecture notes 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;ilargest) //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 } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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 algorithm (see Topic 6 lecture notes
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
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY