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,...
// Java // This method takes an integer array as well as an integer (the starting...
// Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a character stack and converts all lower case letters // to upper case ones....
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...
(C++) Write a function that takes as an input parameter an integer that will represent the...
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number...
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!
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2. 2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a...
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).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT