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...
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 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...
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; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT