1 / 12

# CS 127

CS 127. Searching and Sorting. Searching. Searching is a common and well studied programming problem Searching is the process of looking for a particular value in a collection Problem: We want to find the position in a list where a value occurs list.index(x)

Télécharger la présentation

## CS 127

E N D

### Presentation Transcript

1. CS 127 • Searching and Sorting

2. Searching • Searching is a common and well studied programming problem • Searching is the process of looking for a particular value in a collection • Problem: We want to find the position in a list where a value occurslist.index(x) • Return the index in the list of the first item whose value is x. It is an error if there is no such item.

3. Searching • We want to create a search function which behaves as follows:>>> search (4, [3, 1, 4, 2, 5])2>>> search (7, [3, 1, 4, 2, 5])-1def search(x, nums): try: return nums.index(x) except: return -1

4. Strategy 1: Linear Search • Search through a list one by one until the item is found • This is a simple algorithm and works well for moderately sized listsFor an unordered list, this algorithm is as good as any

5. Linear Search Exercise • Given a list of numbers, write a python search function that will perform a linear search on a specified number. It will return the index of the position of the number in the list if it was found or -1 if it was not founddef search(x, list)x - number you are looking forlist - a list of numbers • e.g.>>> nums = [10, 14, 93, 23, 48]>>> search(nums, 23)2

6. Strategy 2: Binary Search • This only works when the list is orderedSimilar to the principle of the number guessing game- Guess a number between 1 and 100- Each time there is a guess, we will tell if the guess is right or the number is higher or lower- Ideally, first guess will be 50- Then half of range either higher or lower etcBinary search strategy can be applied to look through a sorted list

7. Binary Search Algorithm • Use two variables to track the endpoints of the range in the list where the item might beInitially number can be anywhere in the list, so low is first item in list and high is the last item in the listLoop and look at item in the middle of the range and compare it to xIf it is smaller, high is moved so that search is narrowed to lower half • If it is larger, low is moved so search is narrowed to the upper half • Continue this process until x is found or low > high (no longer any more places to look)

8. Binary Search Implementation • def search(x, nums): low = 0 high = len(nums)-1 while low <=high : #There is still a range to search mid = (low + high) //2 item = nums[mid] #Get the middle item in the current range if x == item : #Found it return mid elif x < item : #x is in the lower half of the rangehigh = mid -1 #move top marker down else: #x is in the upper half low = mid + 1#move bottom marker up return -1 #no range left to search. x is not there

9. Comparing Algorithms

10. Binary Search Example • How many guesses does it take to guess the name of a stranger in New York City provided they are listed in the phone book (We can estimate population of New York is 12 million)Log2 12,000,000 = 23.51Searching a 1, 000, 000 items requires 20 guessesSearching a 2, 000, 000 items requires 21 guessesSearching a 4, 000, 000 items requires 22 guessesSearching a 8, 000, 000 items requires 23 guessesSearching a 16, 000, 000 items requires 24 guesses!!!The binary search algorithm is very powerful

11. Sorting AlgorithmsNative Sorting - Selection Sort • If we have n elements find the smallest value in the list and put it in the 0th positionFind the smallest remaining value from 1-(n-1) and put it in position 1Next, find the smallest from 2 - (n-1) and put it in position 2 etc

12. Native Sorting - Selection Sort • def selSort(nums): • #sort nums into ascending order • n=len(nums) • #for each position in the list, except the very last • for bottom in range(n-1): • print(nums[bottom]) • #find the smallest item in nums[bottom] ...nums[n-1] • mp =bottom #bottom is the smallest initially • for i in range(bottom+1, n): #look at each position • if nums[i] <= nums[mp]: • mp =i #remember its index • #swap smallest item to the bottom • nums[bottom], nums[mp] = nums[mp], nums[bottom] • return nums

More Related