Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Assignment Your assignment is to write a program that creates a single dime

ID: 3766604 • Letter: J

Question

JAVA Assignment

Your assignment is to write a program that creates a single dimensioned array of integers fills it with random numbers, then sorts the array, and does searches in the array.

Your program must first request the user to input an array size. This size may be any integer from 1 to 20,000 (make certain to verify that the number is within this range and if not, ask for a number again and again until you get a number if the correct range). Next you program will create an array of this size and fill the array with random numbers from 1 and the array size. Next it will copy this array into another array that your program crates that is the same size. The program then invokes a method that you will create that sorts the input array (but not the copied one). This method is to have a single input, the array. Next if the size of the array is 16 or less, the program will display the contents of both the sorted and unsorted arrays so that the sort routine can be verified.

Next the program will solicit a search number between 1 and array size from the user (make certain to verify that the number is within this range and if not, ask for a number again and again until you get a number if the correct range).

Next your program will invoke a method you will write that uses a linear search to find the input search number in the sorted array. The method will return the position within the array (0 to array size less 1) if it finds the number. Otherwise it will return a minus one. The program will then display a message giving the location of the find or state that the number was not in the array. Immediately before and after the invoke of the search, your program is to invoke System.nanoTime() (use the Internet to find out how to use this) and save these values in variables of type long. It then subtracts the before time from the after time and displays the difference with a message stating how long the linear search took in milliseconds.

Next your program will invoke a method you will write that uses a binary search to find the search number in the sorted array. The method will return the position within the array (0 to array size less 1) if it finds the number, otherwise it will return a minus one. Immediately before and after the invoke of the search, your program is to invoke System.nanoTime() and save these values. It then subtracts the before time from the after time and displays the difference with a message stating how long the binary search took in milliseconds.

Also make a flow chart of your program and hand it in. Make certain to show all loops, ifs, and processing symbols.

Thank you.

Explanation / Answer

// run this programm and go through it you will get the required out put.
import java.util.Arrays;
import java.util.Scanner;

public class Dimenarray {
  
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n,key;
long a,b,c,d;
int minimum=1;
do{
System.out.println("Enter the array size between 1 and 20000");
n=sc.nextInt();
}while(n>20000 || n<1);
int ar[] = new int[n];
int ar1[] = new int[n];
for(int j=0;j<n;j++)
{
ar[j]=minimum + (int)(Math.random()*n);
}
System.arraycopy(ar, 0, ar1, 0, n);
Arrays.sort(ar);
do{
System.out.println("Enter the number to search between 1 and 20000");
key=sc.nextInt();
}while(key>20000 || key<1);
a=System.nanoTime();
System.out.println("Found at by linear search"+leniarSearch(ar,n,key));
b=System.nanoTime();
System.out.println("Time taken in nano Seconds for linear search"+(b-a));
c=System.nanoTime();
System.out.println("Found at by binary search"+binarySearch(ar,n,key));
d=System.nanoTime();
System.out.println("Time taken in nano Seconds for binary search"+(d-c));
if(n<16)
{
System.out.println("array size is less than 16");
System.out.println("sorted array");
for(int j=0;j<n;j++)
{
System.out.print(" "+ar[j]);
}
System.out.println(" un sorted array ");
for(int j=0;j<n;j++)
{
System.out.print(" "+ar1[j]);
}
}
}
static int leniarSearch(int[] ar,int n,int key)
{
for (int c = 0; c < n; c++)
{
if (ar[c] == key) /* Searching element is present */
{
return c;
}
}
return -1;
}
static int binarySearch(int[] ar,int n,int key)
{
int first,middle,last;
first = 0;
last = n - 1;
middle = (first + last)/2;

while( first <= last )
{
if ( ar[middle] < key )
first = middle + 1;
else if ( ar[middle] == key )
{
return (middle);
}
else
last = middle - 1;

middle = (first + last)/2;
}
if ( first > last )
return -1;
return -1;
}

}