Binary Search Lab

Write a program that performs a binary search on a sorted array, implemented as an apvector.

  1. Your program should set the size of the array to a random number between 20 and 50 and then fill the array with integers. Print the elements, going across the screen. Sort them in ascending order, and print the sorted array. Be sure to label your output.

    Sample output

    Original array of 25 elements:
    10  3 23 18 92 11  6 99 20 42 
    42 13 48 84 33 86 15 11 88 40
     2  7 64 41 77
    
    Sorted array of 25 elements:
     2  3  6  7 10 11 12 13 15 18 
    20 23 33 40 41 42 42 48 64 77
    84 86 88 92 99 
  2. Perform a binary search on the sorted array, counting the number of probes made.

    Sample output

          What entry? 12
              status: found at index 6 after 4 probes   
          What entry? 170
              status: not found after 5 probes 


Continue to:  Unit 2  / Prev  / Next