Winter 2018 CISC101 12/2/2018 CISC101 Reminders

Slides:



Advertisements
Similar presentations
Searching/Sorting Introduction to Computing Science and Programming I.
Advertisements

Searching and Sorting Arrays
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 16: Searching, Sorting, and the vector Type.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Spring 2006CISC101 - Prof. McLeod1 Last Time The File class. Back to methods –Passing parameters by value and by reference. –Review class attributes. –An.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Chapter 16: Searching, Sorting, and the vector Type.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Searching and Sorting Searching algorithms with simple arrays
Chapter 16: Searching, Sorting, and the vector Type
CMPT 120 Topic: Searching – Part 2
Week 9 - Monday CS 113.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Analysis of Algorithms
CMPT 120 Topic: Searching – Part 1
COMP 53 – Week Seven Big O Sorting.
Sorting by Tammy Bailey
Chapter 7 Sorting Spring 14
CISC/CMPE320 - Prof. McLeod
Teach A level Computing: Algorithms and Data Structures
Quicksort 1.
CISC101 Reminders Quiz 2 this week.
Last Class We Covered Data representation Binary numbers ASCII values
Winter 2018 CISC101 11/19/2018 CISC101 Reminders
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Objective of This Course
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Chapter 8 Search and Sort
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
MSIS 655 Advanced Business Applications Programming
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Sorting … and Insertion Sort.
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
Quicksort.
CISC101 Reminders All assignments are now posted.
Analysis of Algorithms
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Quicksort.
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Quicksort.
Presentation transcript:

Winter 2018 CISC101 12/2/2018 CISC101 Reminders Last quiz this week in lab. Topics in slides from last week. Last assignment due last day of class (April 6). Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Today Algorithms, Cont. - Searching: Sequential Search. Binary Search. Start Sorting? Winter 2018 CISC101 - Prof. McLeod

Algorithms, Cont. - Searching CISC101 Algorithms, Cont. - Searching We will look at two algorithms: Sequential Search Brute force! Works on a dataset in any order. Binary Search Fast! Only works on sorted datasets. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Searching in Python We already have searching methods as well as the keywords in and not in. Lists: count(), index(), find() Strings: various find(), count() and index() methods. A search could result in a count of occurrences, a True or False or just the location of the first match. So, why do we need to write our own searching functions? Winter 2018 CISC101 - Prof. McLeod

Searching in Python, Cont. You might need to search datasets in a programming language that does not have this code built-in. Or your dataset structure might not be amenable for use with the built-in methods. So, you need to know these algorithms! Winter 2018 CISC101 - Prof. McLeod

Searching, Cont. Sequential search pseudocode: Loop through dataset starting at the first element until the value of target matches one of the elements. Return the location of the match If a match is not found, raise a ValueError exception. (Note that the list.index() method also raises a ValueError exception if the value is not located…) Winter 2018 CISC101 - Prof. McLeod

Sequential Search As code: def sequentialSearch(numsList, target) : i = 0 size = len(numsList) while i < size : if numsList[i] == target : return i i = i + 1 raise ValueError("Target not found.") Note how len(numsList) is done outside loop. Winter 2018 CISC101 - Prof. McLeod

Sequential Search, Version 2 def sequentialSearch2(numsList, target) : for i in range(len(numsList)) : if numsList[i] == target : return i raise ValueError("Target not found.") Using our trusty for loop. Is this one faster? Can this be coded without using the index, i? Winter 2018 CISC101 - Prof. McLeod

Timing our Search See: TimingSeqSearchDemo.py Look at normal case and worst-case. Note how exception is raised and caught. The farther the target is from the beginning of the dataset, the longer the search takes. Makes sense! Our fastest sequential search is still about 4 times slower than index(). Why? Winter 2018 CISC101 - Prof. McLeod

Other Searches Return True if a match exists. Return a count of how many values match. Return a list of locations that match (not built-in to Python). Return the location of the match searching from the end of the list. … Winter 2018 CISC101 - Prof. McLeod

Aside – Comparing Real Numbers Does 1.00000000001 equal 1? To compare floating point numbers you need to know something about the accuracy of the numbers. Suppose that your numbers are accurate to 1 part in 10,000. Instead of comparing using ==, use something like: if abs(numsList[i] – target) <= 1e-4: return i Winter 2018 CISC101 - Prof. McLeod

Summary The sequential search algorithm works for any kind of dataset in any order. It is a “brute force” technique. You now know how to build your own sequential search function if Python’s built-in searches won’t do the trick. Next: binary search. Winter 2018 CISC101 - Prof. McLeod

Searching an Ordered Dataset How do you find a name in a telephone book? In Exercise 4, part 4.3 you (may have) coded the guessing game. What is the most effective way of guessing the unknown number? Winter 2018 CISC101 - Prof. McLeod

Binary Search Binary search pseudocode. Only works on ordered collections: Locate midpoint of dataset to search. b) Determine if target is in lower half or upper half of dataset. If in lower half, make this half the dataset to search. If in upper half, make this half the dataset to search. Loop back to step a), unless the size of the dataset to search is one, and this element does not match, in which case raise a ValueError exception. Winter 2018 CISC101 - Prof. McLeod

Binary Search – Cont. def binarySearch(numsList, target): low = 0 high = len(numsList) - 1 while low <= high : mid = (low + high) // 2 if target < numsList[mid]: high = mid - 1 elif target > numsList[mid]: low = mid + 1 else: return mid raise ValueError("Target not found.") Winter 2018 CISC101 - Prof. McLeod

Binary Search Animation http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html See also: http://www.youtube.com/watch?v=JQhciTuD3E8 Winter 2018 CISC101 - Prof. McLeod

Binary Search – Cont. For the best case, the element matches right at the middle of the dataset, and the loop only executes once. For the worst case, target will not be found and the maximum number of iterations will occur. Note that the loop will execute until there is only one element left that does not match. Each time through the loop the number of elements left is halved, giving the progression below for the number of elements: Winter 2018 CISC101 - Prof. McLeod

Binary Search – Cont. Number of elements to be searched - progression: The last comparison is for n/2m, when the number of elements is one (worst case). So, n/2m = 1, or n = 2m. Or, m = log2(n). So, the algorithm loops log(n) times for the worst case. Winter 2018 CISC101 - Prof. McLeod

Binary Search - Cont. Binary search with log(n) iterations for the worst case is much better than n iterations for sequential search! Major reason to sort datasets! Winter 2018 CISC101 - Prof. McLeod

Binary Search – Timings See TimingBothSearchesDemo.py Does index() work any faster with a sorted() list? Can index() assume the list is sorted? Compare comparison counts between sequential and binary. Winter 2018 CISC101 - Prof. McLeod

Summary You sort datasets just so you can use binary search – it is so much faster! The search functionality built into Python cannot assume that the dataset is sorted, so it will always take longer than our coded binary search. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview We will look at three simple sorts: Insertion sort CISC101 Sorting – Overview We will look at three simple sorts: Insertion sort Selection sort Bubble sort Insertion and Selection can be very useful in certain situations. Bubble sort may be the *worst* sorting algorithm known! (See: https://www.youtube.com/watch?v=k4RRi_ntQc8) Any of these would be easy to code for smaller datasets. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

Before We Start… You need to learn these algorithms for the same reasons you needed to learn searching algorithms. The sort() in Python is way faster – it uses Quicksort, which uses recursion – both topics are outside the scope of this course (see CISC121!). Even if we coded Quicksort it would still be slower because of the interpreted vs. compiled issue. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. The first step in sorting is to select the criteria used for the sort and the direction of the sort. It could be ascending numeric order, or alphabetic order by last name, etc. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. How to sort (which algorithm to use)?: How large is the dataset? What will be critical: memory usage or execution time? Is it necessary that a new element be inserted in order or can it be added at the end and the sort deferred? How often will the algorithm be asked to sort a dataset that is already in order, except for a few newly added elements? Or, will it always have to re-sort a completely disordered dataset? Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. Sorting algorithms can be compared on the basis of: The number of comparisons for a dataset of size n, The number of data movements (“swaps”) necessary, and How these measures change with n (Analysis of Complexity…). Often need to consider these measures for best case (data almost in order), average case (random order), and worst case (reverse order). Some algorithms behave the same regardless of the state of the data, and others do better depending on how well the data is initially ordered. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. If sorting simple values like integers or key values, then comparisons are easy to carry out and the comparison efficiency of the algorithm may not be as important as the number of data movements. However, if strings or objects are being compared then the number of comparisons would be best kept to a minimum. Finally, the only real measure of what algorithm is the best is an actual measure of elapsed time. The initial choice can be based on theory alone, but the final choice for a time-critical application must be by actual experimental measurement. Winter 2018 CISC101 - Prof. McLeod

Sorting – Overview – Cont. I will be presenting code samples that sort lists of integers into ascending order because this is easiest to understand. However the logic of the algorithm can be applied directly to lists of strings or other objects, provided the search criteria are specified. Descending order usually only requires that you change the comparison from “>” to “<“. Winter 2018 CISC101 - Prof. McLeod