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

Question Details I\'m having trouble with this program. First of all the program

ID: 3639294 • Letter: Q

Question

Question Details
I'm having trouble with this program. First of all the program must do the following

1.I need to create a 1 dimensional Array with 31 integers by using a random number generator, and then output it
2. With the program I need to use sequential search and locate several target values: some or none in the array. Count the number of iterations it takes to locate each target value and the elapsed time of the search process. Then output it
An example given:

long startTime = System.nanoTime();
//the operation to be measured
long endTime = System.nanoTime() – startTIme;

3. Then use selection sort to order the random integers in ascending order, Log the elapsed time to sort the process

4. Use binary search to search each item in the array. Log the number of iterations and elapsed time before each target value is located. Design your output so that the findings are presented in 3 columns: the sorted array, the corresponding number of iterations to locate this calue, and the corresponding elaspsed time.

Then search for a few numbers that are not in your sorted array and reported the number of iterations and elapsed time.

Here's what I got so far


import java.util.Random;
public class searchAndSort
{

public static void main(String args[])
{
int array[] = new int[31];
Random gen = new Random();
for(int i=0; i<31; i++)
{
array[i] = gen.nextInt(100);
}
selection_srt(array, 31);
printArray(array);
System.out.println(binary_search(20, array));
}
public static void printArray(int arr[])
{
for(int i=0; i<arr.length; i++)
System.out.print(arr[i] + " ");
}


public static void selection_srt(int array[], int n)
{
for(int x=0; x<n; x++)
{
int index_of_min = x;
for(int y=x; y<n; y++)
{
if(array[index_of_min]>array[y])
{
index_of_min = y;
}
}
int temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
}
}

public static int binary_search(int key, int[] array)
{
int lo = 0;
int hi = array.length - 1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (key < array[mid]) hi = mid - 1;
else if (key > array[mid]) lo = mid + 1;
else return mid;
}
return -1;

}
}

Explanation / Answer

import java.util.Random; public class Test { public static void main(String args[]) { int arr[] = new int[31]; Random gen = new Random(); long startTime = 0; long endTime = 0; int index = -1; for(int i=0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote