Question 7: Given the sorted array A: #(11 15 24 25 27 35 39 42 54 62 64 67 77 81 94) a. Use binary search to find a = 39 in the array. In your answer, you must show the sub-array elements considered for the search in each pass of the algorithm. b. Explain and express the time complexity of the binary search in terms of the size of the input. At last, show this time complexity with big O notation.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

(Q7) This is a Data Structures problem and the programming language used is Lisp. Solve the question we detailed steps and make it concise and easy to understand. Please and thank you.

Question 7:
Given the sorted array A:
#(11 15 24 25 27 35 39 42 54 62 64 67 77 81 94)
a.
Use binary search to find a =
39 in the array. In your answer,
you must show the sub-array elements considered for the search in each
pass of the algorithm.
b.
Explain and express the time complexity of the binary search
in terms of the size of the input. At last, show this time complexity with
big O notation.
Transcribed Image Text:Question 7: Given the sorted array A: #(11 15 24 25 27 35 39 42 54 62 64 67 77 81 94) a. Use binary search to find a = 39 in the array. In your answer, you must show the sub-array elements considered for the search in each pass of the algorithm. b. Explain and express the time complexity of the binary search in terms of the size of the input. At last, show this time complexity with big O notation.
Expert Solution
Step 1: Lisp Program Approach

The Common Lisp program you provided is a binary search algorithm implementation:

  1. The binary-search function takes two arguments: array (a sorted array to search in) and target (the element to find).

  2. It initializes two variables, left and right, which represent the left and right indices of the current search range. left is initially set to 0, and right is set to the index of the last element in the array (the length of the array minus 1).

  3. The program enters a loop that continues until the left index is greater than the right index. This loop is the core of the binary search algorithm.

  4. Inside the loop, it calculates the mid index, which represents the middle of the current search range. The mid index is computed as the integer result of averaging the left and right indices.

  5. The algorithm checks if the element at the mid index in the array matches the target. If it does, the function returns mid, indicating that the target element has been found.

  6. If the element at the mid index is less than the target, it means that the target must be in the right half of the remaining search space. In this case, it updates the left index to mid + 1.

  7. If the element at the mid index is greater than the target, it means that the target must be in the left half of the remaining search space. In this case, it updates the right index to mid - 1.

  8. If the loop exits without finding the target (when left becomes greater than right), the function returns nil, indicating that the target element is not in the array.

  9. In the example usage section, a sorted array sorted-array and a target element target (in this case, 39) are defined. The binary-search function is called with these arguments, and the result is stored in the result variable.

  10. Finally, the program prints a message to the console using format t. If the result is not nil, it indicates that the target was found, along with the target element and its index in the array. If the result is nil, it indicates that the target was not found in the array.

steps

Step by step

Solved in 7 steps with 3 images

Blurred answer
Knowledge Booster
Hash Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education