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

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 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.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
Write a program in C that takes as input an 8-bit binary number and prints the...
Write a program in C that takes as input an 8-bit binary number and prints the next 10 binary numbers. Define a binary number as int binNum[8]; Use binNum[0] to store the most significant (i.e., leftmost) bit and binNum[7] to store the least significant bit. Ask the user to input the first binary number with each bit separated by at least one space.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT