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...
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 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
// 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 to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
C++ Write a word search program that searches an input data file for a word specified...
C++ Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following function: void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount); void processFile(ifstream &inFile, string wordSearch, int &wordCount, int...
1. What is the Big-O run-time of linear search and binary search in the best, average...
1. What is the Big-O run-time of linear search and binary search in the best, average and worst cases?       Linear Search                  Binary Search Best: Worst: Average: 2. What is the Big-O runtime of the following linked list methods? addFirst() toString() What is the Big-O runtime of the below Stack method? isSorted(Node n) 3. What is the Big-O runtime of the below methods: Method A: Stack Copy Constructor Option 1 public Stack(Stack<T> original) { if(original == null) { return; }...
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT