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

Write a Java application that performs the following task: Create an int array (

ID: 3806898 • Letter: W

Question

Write a Java application that performs the following task:

Create an int array (you can declare the values yourself, or use the Random feature in java to create this array). Sort the data. Prompt user to input an integer from the keyboard. Search for the user input value in the array created in step 1 using a simple linear search. Display a message whether the user input has been found in the array, at what position, and how many steps the program required to search for the data value. Search for the user input value in the array created in step 1 using another (hopefully more efficient) search algorithm. Display a message whether the user input has been found in the array, at what position, and how many steps the program required to search for the data value.

Explanation / Answer

ArraySearchTest.java

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;


public class ArraySearchTest {

  
   public static void main(String[] args) {
       int a[]= new int[10];
       Scanner scan = new Scanner(System.in);
       Random r = new Random();
       for(int i=0; i<a.length; i++){
           a[i] = r.nextInt(100)+1;
       }
       System.out.println("Array elements are ");
       System.out.println(Arrays.toString(a));
       System.out.println("Enter the key: ");
       int key = scan.nextInt();
       int index = linerSearch(a, key);
       System.out.println("Using linear search: Key "+key+" found at index: "+index);
      
       Arrays.sort(a);
       System.out.println("Array elements are after sort: ");
       System.out.println(Arrays.toString(a));
      
       index = binarySearch(a,key);
       System.out.println("Using binary search: Key "+key+" found at index: "+index);
      
   }
   public static int linerSearch(int[] arr, int key){
       int size = arr.length;
       int stepsCount = 0;
       for(int i=0;i<size;i++){
           stepsCount++;
           if(arr[i] == key){
               System.out.println("Number of steps to find in linear: "+stepsCount);
              
               return i+1;
               }
           }
       System.out.println("Number of steps to find in linear: "+stepsCount);
       return -1;
       }

   public static int binarySearch(int[] inputArr, int key) {
       int stepsCount = 0;
       int start = 0;
   int end = inputArr.length - 1;
   while (start <= end) {
       stepsCount++;
   int mid = (start + end) / 2;
   if (key == inputArr[mid]) {
       System.out.println("Number of steps to find in binary: "+stepsCount);
   return mid;
   }
   if (key < inputArr[mid]) {
       end = mid - 1;
   } else {
       start = mid + 1;
   }  
   }
   System.out.println("Number of steps to find in binary: "+stepsCount);
   return -1;
   }
}

Output:

Array elements are
[12, 54, 79, 63, 34, 46, 71, 64, 70, 71]
Enter the key:
34
Number of steps to find in linear: 5
Using linear search: Key 34 found at index: 5
Array elements are after sort:
[12, 34, 46, 54, 63, 64, 70, 71, 71, 79]
Number of steps to find in binary: 2
Using binary search: Key 34 found at index: 1

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