cisc101 reminders notes today
play

CISC101 Reminders & Notes Today Test 3 this week in tutorial - PowerPoint PPT Presentation

CISC101 Reminders & Notes Today Test 3 this week in tutorial From last time Finding minimums and maximums USATs at the beginning of next lecture Slides 31-37 Please attend and fill out an evaluation Timing


  1. CISC101 Reminders & Notes Today • Test 3 this week in tutorial • From last time … – Finding minimums and maximums • USATs at the beginning of next lecture • Slides 31-37 – Please attend and fill out an evaluation – Timing code execution • School of Computing First Year Information Session • Slides 38-42 – Thursday, March 24 th from 5:30-7:00PM – Thursday, March 24 th from 5:30-7:00PM • Sequential Search • Sequential Search – Goodwin Hall, Room 254 • Binary Search • Overview of programs including Computing and the Arts, • Selection Sort (likely …) Biomedical Computing, Cognitive Science and Software Design • Insertion Sort (perhaps …) • Remaining lecture topics have shifted – May not cover GUIs or other Python modules in-depth Winter 2011 CISC101 - Whittaker 1 Winter 2011 CISC101 - Whittaker 2 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Searching in Python Searching in Python - Cont. • We already have searching methods as well as • You might need to search datasets in a the keywords in and not in programming language that does not have these methods or functions built-in – count(…) and index(…) for lists – find(…) , count(…) and index(…) for strings • A search could return different results • Your dataset structure might not be amenable for – A count of occurrences use with the built-in methods – True or False – Just the location of the first match • So, you need to know these algorithms! • So, why do we need to write our own searching functions? Winter 2011 CISC101 - Whittaker 3 Winter 2011 CISC101 - Whittaker 4 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  2. Sequential Search Sequential Search - Cont. • Sequential search pseudocode def sequentialSearch(numsList, target) : i = 0 • Loop through the dataset starting at the first element until size = len(numsList) the value of the target matches one of the elements while i < size : • Return the location of the match if numsList[i] == target : if numsList[i] == target : • If a match is not found, raise ValueError return i i = i + 1 • Note that the aList .index(…) method also raise ValueError("Target not found.") throws a ValueError exception if the value is not located Note how len(numsList) is done outside loop Winter 2011 CISC101 - Whittaker 5 Winter 2011 CISC101 - Whittaker 6 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Sequential Search -Version 2 Timing Our Search • Demo: TimingSeqSearch.py def sequentialSearch2(numsList, target) : for i in range(len(numsList)) : • Note how the exception is raised and caught if numsList[i] == target : return i • The farther the target is from the beginning of the raise ValueError("Target not found.") dataset, the longer the search takes dataset, the longer the search takes – Makes sense! Uses our trusty for loop, but is it faster? • Our fastest sequential search is still 2X slower than aList .index(…) – Why? Winter 2011 CISC101 - Whittaker 7 Winter 2011 CISC101 - Whittaker 8 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  3. Other Search Return Values Searching an Ordered Dataset • How do you find a name in a telephone book? • True if a match exists and False otherwise • A count of how many values match • How do you find a word in a dictionary? • A list of locations that match – Not built-in to Python • In the Week 5 lab, Exercise 3 involved coding a • The location of the match searching from the end • The location of the match searching from the end number guessing game number guessing game of the list, not the beginning – What is the most effective way of guessing the unknown number? Winter 2011 CISC101 - Whittaker 9 Winter 2011 CISC101 - Whittaker 10 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Binary Search Binary Search – Cont. • Binary search pseudocode def binarySearch(numsList, target): low = 0 – Only works on ordered sets high = len(numsList) - 1 while low <= high : a) Locate the midpoint of dataset to search mid = (low + high) // 2 • Test the midpoint to see if it matches if target < numsList[mid]: b) Determine if target is in lower half or upper half of b) Determine if target is in lower half or upper half of high = mid - 1 high = mid - 1 the dataset elif target > numsList[mid]: low = mid + 1 • If in lower half, make that half the dataset to search else: • If in upper half, make that half the dataset to search return mid • Loop back to step a) unless you’ve exhausted all the raise ValueError("Target not found.") possible values • Raise a ValueError exception Winter 2011 CISC101 - Whittaker 11 Winter 2011 CISC101 - Whittaker 12 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  4. Binary Search – Cont. Binary Search – Cont. • Number of elements to be searched (progression) • What is the best case? – The element matches right at the middle of the dataset, n n n n n and the loop only executes once , , , , ..., m 2 3 2 2 2 2 • What is the worst case? • The last comparison is for n/2 m , when the number – target will not be found and the maximum number of iterations will occur iterations will occur of elements is one (worst case) of elements is one (worst case) – So, n/2 m = 1 or n = 2 m • Note that the loop will execute until there is only – m = log 2 (n) one element left that does not match • So, the algorithm loops log(n) times in the worst • Each time through the loop the number of case elements left is halved Winter 2011 CISC101 - Whittaker 13 Winter 2011 CISC101 - Whittaker 14 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Binary Search - Cont. Binary Search – Timing • Binary search with log(n) iterations for the worst • Demo: TimingBothSearches.py case is much better than n iterations for the worst – Much better time now! • Does aList .index(…) work any faster with a case with a sequential search! sorted(…) list? • Can aList .index(…) assume the list is sorted • Major reason to sort datasets! • Major reason to sort datasets! and thus switch to a binary search? • Could aList .index(…) determine if the list is in order and then switch to binary search? – How would it do this? Winter 2011 CISC101 - Whittaker 15 Winter 2011 CISC101 - Whittaker 16 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  5. Sorting Overview Sorting Overview – Cont. • We will look at three simple sorts: • The first step in sorting is to select the criteria – Selection sort used for the sort and the direction of the sort – Insertion sort – Bubble sort • It could be ascending numeric order, or alphabetic order by last name, etc. order by last name, etc. • We might get a quick peek at Quicksort, but you will not be responsible for knowing this one Winter 2011 CISC101 - Whittaker 17 Winter 2011 CISC101 - Whittaker 18 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Choosing a Sorting Algorithm Comparing Sorting Algorithms • Sorting algorithms can be compared using … • How large is the dataset? – the number of comparisons for a dataset of size n • What is critical: memory usage or execution – the number of data movements (“swaps”) necessary time? – how these measures change with n • Will the algorithm be asked to sort … • Complexity analysis! – a dataset that is already in order except for a few • Often need to consider these measures for best • Often need to consider these measures for best newly added elements newly added elements case (data almost in order), average case – a completely disordered dataset? (random order), and worst case (reverse order) – Some algorithms behave the same regardless of the state of the data – Others do better depending on how well the data is initially ordered Winter 2011 CISC101 - Whittaker 19 Winter 2011 CISC101 - Whittaker 20 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

Recommend


More recommend