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 called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
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 function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
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!
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000). Hint:...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000).
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT