




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Algorithm Design and Recursion, Computer Science, Basic Techniques, Efficiency of Algorithms, Process of Looking, Searching Problem, Search Function, Linear Search, Collection of Data, Binary Search are the important key points of lecture slides of Introduction to Computer Science.
Typology: Slides
1 / 133
This page cannot be seen from the preview
Don't miss anything!





























































































Algorithm Design and Recursion
To understand the basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms for linear and binary search. To understand the basic principles of recursive definitions and functions and be able to write simple recursive functions.
particular value in a collection. For example, a program that maintains a membership list for a club might need to look up information for a particular member – this involves some sort of search process.
Here is the specification of a simple searching function: def search(x, nums):
Here are some sample interactions: >>> search(4, [3, 1, 4, 2, 5]) 2 >>> search(7, [3, 1, 4, 2, 5])
We can test to see if a value appears in a sequence using in. if x in nums:
If we want to know the position of x in a list, the index method can be used. >>> nums = [3, 1, 4, 2, 5] >>> nums.index(4) 2
The only difference between our search function and index is that index raises an exception if the target value does not appear in the list. We could implement search using index by simply catching the exception and returning -1 for that case.
Pretend you’re the computer, and you were given a page full of randomly ordered numbers and were asked whether 13 was in the list. How would you do it? Would you start at the top of the list, scanning downward, comparing each number to 13? If you saw it, you could tell me it was in the list. If you had scanned the whole list and not seen it, you could tell me it wasn’t there.
where you search through the list of items one by one until the target value is found. def search(x, nums):for i in range(len(nums)): if nums[i]return == x: # item found, return the index valuei return -1 # loop finished, item was not in list This algorithm wasn’t hard to develop, and works well for modest-sized lists.
If the data is sorted in ascending order (lowest to highest), we can skip checking some of the data. As soon as a value is encountered that is greater than the target value, the linear search can be stopped without looking at the rest of the data. On average, this will save us about half the work.
If the data is sorted, there is an even better searching strategy – one you probably already know! Have you ever played the number guessing game, where I pick a number between 1 and 100 and you try to guess it? Each time you guess, I’ll tell you whether your guess is correct, too high, or too low. What strategy do you use?
Each time we guess the middle of the remaining numbers to try to narrow down the range.
Binary means two, and at each step we are diving the remaining group of numbers into two parts.
We can use the same approach in our binary search algorithm! We can use two variables to keep track of the endpoints of the range in the sorted list where the number could be. Since the target could be anywhere in the list, initially low is set to the first location in the list, and high is set to the last.
The loop terminates when either x is found There are no more places to look (low > high)
def search(x, nums): low = 0 high = len(nums) - 1 while low <= high: # There is still a range to search mid = (low + high)//2 # Position of middle item item = nums[mid] if x == item: return mid # Found it! Return the index elif x < item: high = mid - 1 # x is in lower# move top marker down half of range else: low = mid + 1 # x is in upper# move bottom marker up half of range return -1 # No range left # x is not there to search,