GoConqr has a new look! Let us know what you think.

Quiz

Untitled

Adrian Mendoza 4 questions

Take the quiz

Questions in this quiz (4)

  1. 1True or false: If an array is already sorted, Linear Search / Sequential Search is more efficient than Binary Search.
    1. Atrue
    2. Bfalse
  2. 2What must be true before performing a binary search?
    1. AThe elements must be sorted.
    2. BIt can only contain binary values.
    3. CThe elements must be some sort of number (i.e. int, double, Integer)
    4. DThere are no necessary conditions.
  3. 3Which of the following functions is a proper implementation of binary search?
    1. Apublic static int binarySearch(int[] array, int key)
      {
      int n = array.length;
      int first = 0;
      int last = n - 1;
      int middle = (first + last)/2;

      while( first <= last )
      {
      if ( array[middle] < key )
      {
      first = middle + 1;
      }
      else if ( array[middle] == key )
      {
      return (middle + 1);
      }
      else
      {
      last = middle - 1;
      }

      middle = (first + last)/2;
      }
      return -1;
      }
    2. Bpublic static int binarySearch(int[] array, int key)
      {
      int n = array.length;
      int first = 0;
      int last = n - 1;
      int middle = (first + last)/2;

      while( first <= last )
      {
      if ( array[middle] < key )
      {
      first = middle + 1;
      }
      else if ( array[middle] == key )
      {
      return middle;
      }
      else
      {
      last = middle - 1;
      }

      middle = (first + last)/2;
      }
      return -1;
      }
    3. Cpublic static int binarySearch(int[] array, int key)
      {
      n = array.length;
      first = 0;
      last = n - 1;
      middle = (first + last)/2;

      while( first <= last )
      {
      if ( array[middle] < key )
      {
      first = middle + 1;
      }
      else if ( array[middle] == key )
      {
      System.out.println(key + " found at index " + middle);
      }
      else
      {
      last = middle - 1;
      }
      middle = (first + last)/2;
      }
      System.out.println("Key not found!")
      }
    4. Dpublic static int binarySearch(int[] array, int key)
      {
      int n = array.length;
      int first = 0;
      int last = n - 1;
      int middle = (first + last)/2;

      while( first <= last )
      {
      if ( array[middle] < key )
      {
      first = middle + 1;
      }
      else if ( array[middle] == key )
      {
      int result = middle;
      }
      else
      {
      last = middle - 1;
      }

      middle = (first + last)/2;
      }
      }
  4. 4Given this array:

    1 2 4 5 6 7 8 12 14 21 22 42 53
    How many comparisons are required to find 42 using the Linear Search / Sequential Search?
    1. A3
    2. B2
    3. C12
    4. D5

Information

Description
Quiz on Untitled, created by Adrian Mendoza on 01/02/2023.
Language
English
Views
0
Rating
No ratings yet
Rate this quiz
Rate this quiz

Feedback

What is it about?

What do you think of the new look?

How do you like it?