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 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 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...
write JAVA code with the following condition Write the pseudocode for a new data type MyStack...
write JAVA code with the following condition Write the pseudocode for a new data type MyStack that implements a stack using the fact that you have access to a queue data structure with operations enqueue(), dequeue(), isEmpty(). Remember that every stack should have the operations push() and pop(). Hint: use two queues, one of which is the main one and one is temporary. Please note that you won’t be able to implement both push() and pop() in constant time. One...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT