Download presentation
Presentation is loading. Please wait.
Published byKory Barber Modified over 9 years ago
1
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
2
2 Section 2.1 Algorithms An algorithm is a definite procedure for solving a problem using a finite number of steps.
3
3 Properties of Algorithms Input - An algorithm has input values from a specified set. Output - From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem. Definiteness - The steps of an algorithm must be defined precisely.
4
4 Properties of Algorithms Finiteness - An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set. Effectiveness - It must be possible to perform each step of an algorithm exactly and in a finite amount of time.
5
5 Properties of Algorithms Generality - The procedure should be applicable for all problems of the desired form, not just for a particular set of input values
6
6 Maximum Element in a Finite Set procedure max(a 1, a 2, …, a n :integers) max := a 1 for i :=2 to n if max < a i then max := a i {max is the largest element}
7
7 Searching Algorithm procedure linear search(x:integer, a 1, a 2, …, a n : distinct integers) i := 1 while (i ai) i := i + 1 if i <= n then location :=i else location := 0 { location is the subscript of the term that equal x, or is 0 if x is not found}
8
Binary Search 4 8 11 23 35 45 55 62 67 78 89 92 0 1 2 3 4 5 6 7 8 9 10 11 For this algorithm to work, the data must be sorted in the array.
9
Binary Search 4 8 11 23 35 45 55 62 67 78 89 92 0 1 2 3 4 5 6 7 8 9 10 11 Suppose the item of interest has value 62! The algorithm finds the midpoint of the array (0+11)/2 = 5 and looks in that position. If the item is found, the search is over
10
Binary Search 4 8 11 23 35 45 55 62 67 78 89 92 0 1 2 3 4 5 6 7 8 9 10 11 In this case, the item wasn’t found. However, since the array is sorted and 62 > 45, we know it should be below the midpoint, if it exists. We now look at the midpoint of what is left by calculating the index (6+11)/2 = 8, and look there.
11
Binary Search 4 8 11 23 35 45 55 62 67 78 89 92 0 1 2 3 4 5 6 7 8 9 10 11 If the item is found, the search is over. In this case, the search goes on.
12
Binary Search 4 8 11 23 35 45 55 62 67 78 89 92 0 1 2 3 4 5 6 7 8 9 10 11 We know 62 < 67 so we should restrict our search to the portion above that has not been searched. If we recalculate the search index (6+7)/2=6 we see that we are narrowing down on the location.
13
Binary Search 4 8 11 23 35 45 55 62 67 78 89 92 0 1 2 3 4 5 6 7 8 9 10 11 But, we are still not there. There is really only one place left to look but the algorithm doesn’t know this. It only knows that 62>55 so it should look below the current location.
14
Binary Search 4 8 11 23 35 45 55 62 67 78 89 92 0 1 2 3 4 5 6 7 8 9 10 11 One final calculation of the index (7+7)/2 = 7 leads us to the location we have been looking for. Note that we only repeated the process and comparison four times (looking in 5, 8, 6, 7) and that is pretty good when there are actually 11 cells.
15
15 Sorting Algorithms Bubble Sort Insertion Sort Selection Sort Quick Sort Heap Sort Radix Sort Shell Sort
16
16 Greedy Algorithms Algorithms that make what seems to be the “best” choice at each step
17
17 finished
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.