Download presentation
Presentation is loading. Please wait.
Published byPhilomena Clark Modified over 9 years ago
1
Chapter 3 3.1 Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors 3.6 Integers and Algorithms 3.7 Applications of Number Theory 3.8 Matrices 1
2
Chapter 3 3.1 Algorithms –Searching Algorithms –Greedy Algorithms –The Halting Problem 2
3
Algorithm Definition 1: An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. Example 1: Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. 3
4
We perform the following steps 1.Set the temporary maximum equal to the first integer in the sequence. (the temporary maximum will be the largest integer examined at any stage of the procedure.) 2.Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer. 3.Repeat the previous step if there are more integers in the sequence. 4.Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. 4
5
Pseudocode Pseudocode provides an intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language. Algorithm 1: Finding the maximum element in a finite sequence. 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} 5
6
Property of Algorithm Input. Output. Definiteness. The steps of an algorithm must be defined precisely. Correctness. Finiteness. Effectiveness. Generality. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values. 6
7
Searching Algorithms Search Problem: Locating an element in an (ordered) list. Linear search Binary search (ordered list) 7
8
The linear search Algorithm 2 : the linear search algorithm procedure linear search (x: integer, a 1, a 2, …,a n : distinct integers) i :=1; while ( i ≤n and x ≠ a i ) i := i + 1 If i ≤ n then location := i Else location := 0 {location is the subscript of the term that equals x, or is 0 if x is not found} 8
9
The binary search Algorithm 3: the binary search algorithm Procedure binary search (x: integer, a 1, a 2, …,a n : increasing integers) i :=1 { i is left endpoint of search interval} j :=n { j is right endpoint of search interval} While i < j begin m := (i+j)/2 if x > a m then i := m+1 else j := m end If x = a i then location := i else location :=0 {location is the subscript of the term equal to x, or 0 if x is not found} Example 3: to search for 19 in the list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 Example 3: to search for 19 in the list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 9
10
Sorting Sort: –Sorting is putting elements into a list in which the elements are in increasing order. E.g. 1)7,2,1,4,5,9 -> 1,2,4,5,7,9 2)d,h,c,a,f -> a,c,d,f,h. Bubble sort Insertion sort 10
11
Bubble Sort ALGORITHM 4: The Bubble Sort procedure bubble sort (a 1, a 2, …,a n : real numbers with n ≥2) for i := 1 to n-1 for j := 1 to n- i if a j > a j+1 then interchange a j and a j+1 {a 1, a 2, …,a n is in increasing order} Example 4: Use the sort to put 3, 2, 4, 1, 5 into increasing order. 11
12
Bubble Sort
13
Insertion Sort Algorithm 5: The Insertion Sort procedure insertion sort (a 1, a 2, …,a n : real numbers with n ≥2) for j := 2 to n begin i := 1 while a j > a i i := i + 1 m := a j for k :=0 to j-i-1 a j-k := a j-k-1 a i := m end {a 1, a 2, …,a n are sorted} Example 5: Use the insertion sort to put the elements of the list 3, 2, 4, 1, 5 into increasing order. Example 5: Use the insertion sort to put the elements of the list 3, 2, 4, 1, 5 into increasing order. 13
14
Greedy Algorithm Optimization Problem: find the best solution. Algorithms that make what seems to be the best choice at each step are called greedy algorithms. 14
15
Example 6: Consider the problem of making n cents change with quarters, dimes, nickels, and pennies, and using the least total number of coins. Algorithm 6: Greedy Change-Marking Algorithm procedure change (c 1, c 2, …, c r : values of denominations of coins, where c 1 > c 2 > … > c r ; n: a positive integer) for i := 1 to r while n ≥ c i begin add a coin with value c i to the change n := n – c i end 15
16
The Halting Problem There is a problem that cannot be solved using any procedure. That is, there are unsolvable problems. Halting Problem FIGURE 2 Showing that the Halting Problem is Unsolvable. 16
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.