Question

In: Computer Science

Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined length

Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined length

JAVA

Solutions

Expert Solution

import java.util.*;               // for using Scanner class
class LinearSearch
{
   public static void main(String args[])
   {
       Scanner obj = new Scanner(System.in);               // making Scanner class object
       System.out.print("How many elements do you want to enter : ");           // asking for input size
       int size = obj.nextInt();               // taking input of size
       System.out.println("\nEnter the elements of the array : ");               // asking for entering array elements
       ArrayList arr = new ArrayList<>();               // creating dynamic array of undetermined size which could take any number of input for array
       for(int i=0;i        {
           int x = obj.nextInt();               // taking input of array elements
           arr.add(x);               // adding input to dynamic array
       }
       System.out.println("\nEnter the key value to search in the array : ");           // Enter key value to search in the array
       int key = obj.nextInt();           // taking input of key to be searched
       int found=0;               // for status of key found or not
       int index=0;               // storing index of key to be found
       for(int i=0;i        {
           if(arr.get(i)==key)           // if key found in the array
           {
               found=1;           // status of found = 1
               index = i;               // storing index of key in the array
               break;              
           }
       }
       if(found==1)           // if key found in the array
           System.out.println("Key found at index "+index);
       else           // if key not found in the array
           System.out.println("Key not found in the array");
   }
}

Output :


Related Solutions

Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
Write a linear-search Java method to test if a String array is already sorted alphabetically. The...
Write a linear-search Java method to test if a String array is already sorted alphabetically. The prototype should be static boolean ifsorted(String [] s) { }
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
Write a Pseudo Code to send an Array of 20 elements from 8051 to the computer...
Write a Pseudo Code to send an Array of 20 elements from 8051 to the computer via serial port at maximum baud rate possible with XTAL=11.0592MHz.
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in an array. NOT in linked List. Implement an array-based Linked List in your language. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE...
Create an array-based implementation of a binary tree. DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
Create an array-based implementation of a binary tree. DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT