Question

In: Computer Science

Write a program to show the difference between linear search and binary search. Show the input...

Write a program to show the difference between linear search and binary search. Show the input test data for your program and the output produced by your program which clearly show that binary search is faster than linear search

Solutions

Expert Solution

import java.util.*;

public class ArraySearch {

  public static boolean LinearSearch(int arr[], int x) {

    int n = arr.length;

    for (int i = 0; i < n; i++) {

      if (arr[i] == x) return true;

    }

    return false;

  }

  public static boolean binarySearch(int arr[], int x) {

    int l = 0, r = arr.length - 1;

    while (l <= r) {

      int m = l + (r - l) / 2;

      if (arr[m] == x) return true;

      if (arr[m] < x) l = m + 1; else r = m - 1;

    }

    return false;

  }

  public static int[] CreateArray(int n) {

    int Arr[] = new int[n];

    Random R = new Random();

    for (int i = 0; i < n; i++) {

      Arr[i] = R.nextInt() % 100000 + 1;

    }

    return Arr;

  }

  public static void main(String[] args) {

    double T1, T2, T;

    for (int n = 10; n <= 100000; n = n * 10) {

      int[] Arr = CreateArray(n);

      Random R = new Random();

      int number = R.nextInt() % 100000 + 1;

      T1 = System.nanoTime();

      boolean found = LinearSearch(Arr, number);

      T2 = System.nanoTime();

      T = T2 - T1;

      System.out.print(n + " Linear_Search : " + T + " nanosecond ");

      T1 = System.nanoTime();

      found = binarySearch(Arr, number);

      T2 = System.nanoTime();

      T = T2 - T1;

      System.out.println(" Binary_Search : " + T + " nanosecond");

    }

  }

}

//SAMPLE OUTPUT

******************************************************************************************
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION
******************************************************************************************


Related Solutions

Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Write a function that takes a binary search tree as input and produces a linked list...
Write a function that takes a binary search tree as input and produces a linked list of the entries, with the entries sorted (smallest entries at the front of the list and largest entries at the back). *Hint: use in-order traversal.* C++ there is no any structure
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
Write a program that read the input from user and convert it to binary and hex...
Write a program that read the input from user and convert it to binary and hex in C language.
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT