Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching/Sorting Introduction to Computing Science and Programming I.

Similar presentations


Presentation on theme: "Searching/Sorting Introduction to Computing Science and Programming I."— Presentation transcript:

1 Searching/Sorting Introduction to Computing Science and Programming I

2 Searching Searching is the general problem of finding a specific item within a collection of items. Searching is the general problem of finding a specific item within a collection of items. We are only going to look at searching through a list of items. There are other data structures that may require different strategies We are only going to look at searching through a list of items. There are other data structures that may require different strategies

3 Searching Linear search: This is the simplest as most obvious way to search. Look through a list one item at a time until the item is found or the end of the list is reached. Linear search: This is the simplest as most obvious way to search. Look through a list one item at a time until the item is found or the end of the list is reached.

4 Searching def search(lst, val): """Find the first occurrence of val in lst. Return its index or -1 if not there. >>> search([0, 10, 20, 30, 40], 30) 3 >>> search([0, 10, 20, 30, 40], 25) """ for i in range(len(lst)): if lst[i]==val: # we only care about the first match, # so if we’ve found one, return it. return i # if we get this far, there is no val in lst. return -1

5 Searching What is the worst-case running time of linear search? What is the worst-case running time of linear search? If there are n items in the list then the algorithm may have to check each one so it has a running time of n. If there are n items in the list then the algorithm may have to check each one so it has a running time of n. This isn’t bad unless you need to do a lot of searching. In this case there’s a better algorithm that can be used, but needs the list to be sorted. This isn’t bad unless you need to do a lot of searching. In this case there’s a better algorithm that can be used, but needs the list to be sorted.

6 Searching If you think about looking up numbers in a phone book or words in a dictionary with millions of entries you can see how much having sorted information can speed up search. If you think about looking up numbers in a phone book or words in a dictionary with millions of entries you can see how much having sorted information can speed up search. To take advantage of a sorted list we can us a Binary Search algorithm. To take advantage of a sorted list we can us a Binary Search algorithm.

7 Searching Binary Search follows the same strategy that we used for the fast version of the guessing game. Binary Search follows the same strategy that we used for the fast version of the guessing game. Since the list is sorted, if we look at one item, we know that every item after it is larger, and every item before it is smaller. Since the list is sorted, if we look at one item, we know that every item after it is larger, and every item before it is smaller. So if we look at the item in the middle of the list we can know which half of the list the item we’re looking for would be in. So if we look at the item in the middle of the list we can know which half of the list the item we’re looking for would be in.

8 Searching def binary_search(lst, val): # keep track of the first and last possible positions. first = 0 last = len(lst)-1 while first <= last: mid = (first+last)/2 if lst[mid] == val: # found it return mid elif lst[mid] < val: # too small, only look at the right half first = mid+1 else: # lst[mid] > val # too large, only look at the left half last = mid-1 # if we get this far, there is no val in lst. return -1

9 Searching Due to this strategy of halving the possibilities with each try, the algorithm has a running time of log 2 n. Due to this strategy of halving the possibilities with each try, the algorithm has a running time of log 2 n. This is much faster than linear search, but to use binary search you must maintain a sorted list. Every time you insert an item it can take up to n steps. Therefore if you insert a lot of items compared to how many times you search, it may not be worthwhile. This is much faster than linear search, but to use binary search you must maintain a sorted list. Every time you insert an item it can take up to n steps. Therefore if you insert a lot of items compared to how many times you search, it may not be worthwhile. However, if you search more often than you insert items, binary search provides a significant speedup. However, if you search more often than you insert items, binary search provides a significant speedup.

10 Sorting There are an array of sorting algorithms that have been created, we’re going to look at a couple of the simpler ones to start. There are an array of sorting algorithms that have been created, we’re going to look at a couple of the simpler ones to start. In terms of running time, the best general sorting algorithms including the one used by Python’s list.sort() are n log(n). Those algorithms are more complex than what we’ll see today. In terms of running time, the best general sorting algorithms including the one used by Python’s list.sort() are n log(n). Those algorithms are more complex than what we’ll see today.

11 Sorting Selection Sort Selection Sort The basic strategy is to look through your list of items and find the one that goes first. Set it aside and look through the remaining items for the smallest of those. Add this to the first and repeat the process until all the items are sorted. The basic strategy is to look through your list of items and find the one that goes first. Set it aside and look through the remaining items for the smallest of those. Add this to the first and repeat the process until all the items are sorted. ‘Select’ the item that will go next each time you repeat ‘Select’ the item that will go next each time you repeat

12 Sorting In each iteration of the process we will swap the smallest element with the element whose position it will take. In each iteration of the process we will swap the smallest element with the element whose position it will take. 6, 34, 12, 7, 3, 26, 19 Smallest: 3 Swap: 3 and 6 6, 34, 12, 7, 3, 26, 19 Smallest: 3 Swap: 3 and 6 3, 34, 12, 7, 6, 26, 19 Smallest: 6 Swap: 6 and 34 3, 34, 12, 7, 6, 26, 19 Smallest: 6 Swap: 6 and 34 3, 6 12, 7, 34, 26, 19 Smallest: 7 Swap: 7 and 12 3, 6 12, 7, 34, 26, 19 Smallest: 7 Swap: 7 and 12 3, 6, 7 12, 34, 26, 19 Smallest: 12 Swap: 12 and 12 3, 6, 7 12, 34, 26, 19 Smallest: 12 Swap: 12 and 12 3, 6, 7, 12 34, 26, 19 Smallest: 19 Swap: 19 and 34 3, 6, 7, 12 34, 26, 19 Smallest: 19 Swap: 19 and 34 3, 6, 7, 12, 19 26, 34 Smallest: 26 Swap: 26 and 26 3, 6, 7, 12, 19 26, 34 Smallest: 26 Swap: 26 and 26 3, 6, 7, 12, 19, 26, 34 3, 6, 7, 12, 19, 26, 34 The last item will already be in the correct location The last item will already be in the correct location

13 Sorting Selection Sort Pseudocode Selection Sort Pseudocode for every element e from the list, for every element f from e to the end of the list, if f < smallest, set smallest to f swap smallest and e

14 Sorting def selection_sort(lst): ""“ Sort lst in-place using selection sort """ for pos in range(len(lst)): # get the next smallest in lst[pos] # find the next smallest small = lst[pos] # smallest value seen so far smallpos = pos # position of small in lst for i in range(pos+1, len(lst)): # check each value, searching for one # that’s smaller than the current smallest. if lst[i] < small: small = lst[i] smallpos = i # swap it into lst[pos] lst[pos], lst[smallpos] = lst[smallpos], lst[pos]

15 Sorting Running time of selection sort Running time of selection sort If you look at how many times the inner loop repeats for each position it takes n steps for the first, n-1 for the second, and so forth down to 1. So we have n + n-1 + n-2… + 2 + 1 steps which as we’ve seen before works out to n 2 /2 – n/2. If you look at how many times the inner loop repeats for each position it takes n steps for the first, n-1 for the second, and so forth down to 1. So we have n + n-1 + n-2… + 2 + 1 steps which as we’ve seen before works out to n 2 /2 – n/2. The running time is n 2 The running time is n 2

16 Sorting Insertion Sort Insertion Sort The basic idea is to keep a sorted group of elements and repeatedly insert the next element into the correct spot in the sorted group. The basic idea is to keep a sorted group of elements and repeatedly insert the next element into the correct spot in the sorted group. This is similar to how people often sort index cards or files, inserting one at a time into those already sorted. This is similar to how people often sort index cards or files, inserting one at a time into those already sorted.

17 Sorting At the start you consider the first element to be your sorted list At the start you consider the first element to be your sorted list 6, 34, 12, 7, 3, 26, 19 Insert 34 after 6 6, 34, 12, 7, 3, 26, 19 Insert 34 after 6 6, 34, 12, 7, 3, 26, 19 Insert 12 after 6 6, 34, 12, 7, 3, 26, 19 Insert 12 after 6 6, 12, 34, 7, 3, 26, 19 Insert 7 after 6 6, 12, 34, 7, 3, 26, 19 Insert 7 after 6 6, 7, 12, 34, 3, 26, 19 Insert 3 before 6 6, 7, 12, 34, 3, 26, 19 Insert 3 before 6 3, 6, 7, 12, 34, 26, 19 Insert 26 after 12 3, 6, 7, 12, 34, 26, 19 Insert 26 after 12 3, 6, 7, 12, 26, 34, 19 Insert 19 after 12 3, 6, 7, 12, 26, 34, 19 Insert 19 after 12 3, 6, 7, 12, 19, 26, 34 3, 6, 7, 12, 19, 26, 34

18 Sorting We won’t fully analyze it, but insertion sort turns out to have the same running time as selection sort, n 2 We won’t fully analyze it, but insertion sort turns out to have the same running time as selection sort, n 2 Algorithms with running time n 2 are fine with small amounts of data, but faster algorithms are used otherwise. Algorithms with running time n 2 are fine with small amounts of data, but faster algorithms are used otherwise.


Download ppt "Searching/Sorting Introduction to Computing Science and Programming I."

Similar presentations


Ads by Google