In: Computer Science
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);
}
}
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