Question

In: Computer Science

Write a method that takes an integer array as its parameter and sorts the contents of...

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. Call this method 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.

CODE SO FAR:

import java.util.*;

public class ArrayInteger {
public static void getRandomNumber(int A[],int n){
Random generator = new Random();
int nextRand;
for(int i=0;i<n;i++){
nextRand = 1+generator.nextInt(999);
A[i]=nextRand;
}
}
public static int smallestNumber(int A[],int n) {
int smallest=A[0];
for(int i=0;i<n;i++) {
if(A[i]<smallest)
smallest=A[i];
}
return smallest;
}
public static int largestNumber(int A[],int n) {
int largest=A[0];
for(int i=0;i<n;i++) {
if(A[i]>largest)
largest=A[i];
}
return largest;
}
public static int sumOfArray(int A[],int n) {
int sum=0;
for(int i=0;i<n;i++)
sum+=A[i];
return sum;
}
public static float averageOfArray(int A[],int n) {
float avg=(float)sumOfArray(A,n)/n;
return avg;
}
public static void printResult(int A[],int n) {
int count=0;
for(int i=0;i<n;i++) {
System.out.println(A[i]+"\t");
count++;
if(count==5) {
System.out.println("\n");
count=0;
}
}
System.out.println("\n");
System.out.println("Smallest number: "+smallestNumber(A,n));
System.out.println("Largest number: "+largestNumber(A,n));
System.out.println("Average: "+averageOfArray(A,n));
}
//main() function
public static void main(String args[]) {
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array: ");
n=in.nextInt();
int A[]=new int[n];
getRandomNumber(A,n);
printResult(A,n);
}
}

Solutions

Expert Solution

Please do upvote if you found the solution useful. Your feedback is important to me.

Program

import java.util.*;

public class ArrayInteger
{
public static void getRandomNumber(int A[],int n)
{
Random generator = new Random();
int nextRand;
for(int i=0;i<n;i++){
nextRand = 1+generator.nextInt(999);
A[i]=nextRand;
}
}

public static int smallestNumber(int A[],int n)
{
int smallest=A[0];
for(int i=0;i<n;i++)
{
if(A[i]<smallest)
smallest=A[i];
}
return smallest;
}

public static int largestNumber(int A[],int n)
{
int largest=A[0];
  
for(int i=0;i<n;i++)
{
if(A[i]>largest)
largest=A[i];
}
return largest;
}

public static int sumOfArray(int A[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
sum+=A[i];
return sum;
}

public static float averageOfArray(int A[],int n)
{
float avg=(float)sumOfArray(A,n)/n;
return avg;
}

public static void i_sort(int arr[], int n)
{
int count = 0;
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
  
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
  
arr[j + 1] = key;
}
  
for(int i=0;i<n;i++)
{
System.out.println(arr[i]+"\t");
count++;
if(count==5)
{
System.out.println("\n");
count=0;
}
}

}

public static void printResult(int A[],int n)
{
int count=0;
for(int i=0;i<n;i++)
{
System.out.println(A[i]+"\t");
count++;
if(count==5)
{
System.out.println("\n");
count=0;
}
}
System.out.println("\n");
System.out.println("Smallest number: "+smallestNumber(A,n));
System.out.println("Largest number: "+largestNumber(A,n));
System.out.println("Average: "+averageOfArray(A,n));
}

//main() function
public static void main(String args[])
{
int n;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array: ");
n=in.nextInt();
int A[]=new int[n];
getRandomNumber(A,n);
printResult(A,n);
System.out.println("Sorted array is");
i_sort(A,n);
}
}

Program Logic

Insertion sort is the sorting mechanism where the sorted array is built having one item at a time. The array elements are compared with each other sequentially and then arranged simultaneously . In the following program i_sort is used to sort and print the array. Here we choose a key in the array, then we move the other elements by one position to the right if they are greater than the key.

Program Output


Related Solutions

Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
In an application write a method filterStack that takes a stack of integers as a parameter...
In an application write a method filterStack that takes a stack of integers as a parameter and filters its elements (in a new Stack) in a way that places the even elements at the bottom and the odd ones at the top. The original stack should remain unchanged. You should use a queue (only one queue) as a temporary storage. Use stack and queue operations only to solve this problem. No need to write the main method. For example, if...
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws a square of length s. The square should be filled by a randomized color. Then write another function, that calls the shape function to draw 4 squares as a 2x2 table. Please answer in python.
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The method finds and returns an array which contains the positive numbers. For example, if the method receives an array with the following elements: 0 3 -1 2 5 1 -5 2 -2 0 the method should return an array with the following elements: 3 2 5 1 2 In the main method, randomly generate an array of 10 random integers between -5 and 5,...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT