Download presentation
Presentation is loading. Please wait.
1
CSCE 222 Discrete Structures for Computing
Algorithms
2
Quiz 5 Put away everything except a pencil (or pen if feeling bold)
15 minutes Instructor will go over quiz solution after collecting all quizzes Pass to center with name in upper right at end of 15 minutes
3
Search Search Linear Search Binary Search
Find a given element in a list. Return the location of the element in the list (index), or 0 if not found. Linear Search Compare key (element being searched for) with each element in the list until a match is found, or the end of the list is reached. Binary Search Compare key only with elements in certain locations. Split list in half at each comparison. Requires list to be sorted. Talk about where search shows up in CS: all over the place (optimization, AI, ML, etc.)
4
Linear Search Procedure πππππππππππβ Input: πππ¦: integer , π 1 ,β¦, π π : list Output: index of key or 0 if not found for each element a i if π π ==πππ¦ then return π return 0 // πππ¦ was not found
5
Linear Search Exercise
19, 1, 17, 2, 11, 13, 7, 9, 10, 5, 15, 6, 14, 20, 16, 12, 4, 18, 3, 8 Donβt belabor the point. How many comparisons to find: 7? 21?
6
Binary Search Procedure ππππππ¦πππππβ Input: πππ¦: integer , π 1 ,β¦, π π : list in ascending order Output: index of key or 0 if not found if list is empty, return 0 if list has a single element π π then if π π ==πππ¦, then return π else return 0 if key < middle element, then binarySearch left half of list else binarySearch right half of list
7
Binary Search Exercise
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 How many comparisons to find: 7? 21? Do phonebook?
8
Sort Sort Bubble Sort Insertion Sort
Put the elements of a list in ascending order Bubble Sort Compare every element to its neighbor and swap them if they are out of order. Repeat until list is sorted. Insertion Sort For each element of the unsorted portion of the list, insert it in sorted order in the sorted portion of the list. Sorting is used all over the place
9
Bubble Sort Input: π 1 ,β¦, π π : list Output: list in ascending order
Procedure ππ’ππππππππ‘ Input: π 1 ,β¦, π π : list Output: list in ascending order for πβ1 to πβ1 for πβ1 to πβπ if π π > π π+1 then swap π π and π π+1 // π 1 ,β¦, π π is in sorted order. j =1 to n-1 takes advantage of the fact that the end of list is sorted (making it a sort of selection sort)
10
Bubble Sort Exercise 10, 2, 1, 5, 3 10, 2, 1, 5, 3 compare 10,2, swap
1, 2, 3, 5, 10 done 10 compares
11
Insertion Sort Procedure πππ πππ‘πππππππ‘ Input: π 1 ,β¦, π π : list Output: list in ascending order for π=2 to π πβπ while π>1 and π πβ1 > π π swap π π and π πβ1 π βπ β 1 // π 1 ,β¦, π π is in sorted order. Like a bubble sortβ¦ each element bubbles down to its sorted order
12
Insertion Sort Exercise
10, 2, 1, 5, 3 10, 2, 1, 5, 3 compare 10,2, swap 2, 10, 1, 5, 3 compare 10,1, swap 2, 1, 10, 5, 3 compare 2,1, swap 1, 2, 10, 5, 3 compare 10,5, swap 1, 2, 5, 10, 3 compare 2,5 1, 2, 5, 10, 3 compare 10,3, swap 1, 2, 5, 3, 10 compare 5,3, swap 1, 2, 3, 5, 10 compare 2,3 1, 2, 3, 5, 10 done 8 compares
13
Greedy Algorithms The goal of an optimization problem is to maximize or minimize an objective function. One of the simplest approaches to solving optimization problems is to select the βbestβ choice at each step. May always find optimal solution, or a counterexample may exist (a case where the optimal solution is not found).
14
Greedy Change-Making Procedure πβππππ Input: π 1 , π 2 ,β¦, π π : denominations of coins where c 1 > c 2 >β―> π π , π:positive integer Output: π 1 , π 2 ,β¦, π π : the number of coins of each denomination for πβ1 to π π π β0 // the number of π π coins used while πβ₯ π π π π β π π +1 // add a coin of denomination π π π βπβ π π // π π is the number of π π coins used Goal: use fewest coins possible Give max quarters, then max dimes, then max nickels, then pennies. Proof of optimality is in the book.
15
Make Change 69 cents: 56 cents: 69 = 2*25 + 1*10 + 1*5 + 4*1
56 = 2*25 + 1*5 + 1*1
16
The Halting Problem Is there a procedure that does the following: Takes as input a program and input to that program Determines whether that program will eventually stop when run on that input For any program and input No. And I can prove it. Turing proved it in 1936. Has implications for lots of problems in CS, e.g. malware detection, testing
17
Scooping the Loop Snooper
18
Thanks and Gig βem! Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.