CS 102 Lecture 2: Recursion-2

7 views10 pages
5 Mar 2023
Department
Course
Professor

Document Summary

In this module, we are going to understand how to solve different kinds of problems using recursion. In a nutshell, this search algorithm takes advantage of a collection of elements that are already sorted by ignoring half of the elements after just one comparison. You are given a target element x and a sorted array. You need to check if x is present in the array. Compare x with the middle element in the array. If x is the same as the middle element, we return the index of the middle element. Else if x is greater than the mid element, then x can only lie in the right (greater) half subarray after the mid element. Thus, we apply the algorithm, recursively, for the right half. Else if x is smaller, the target x must lie in the left (lower) half. So we apply the algorithm, recursively, for the left half.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions