The document discusses algorithms for searching lists of elements. It describes the linear search algorithm, which sequentially compares the target element to each element in the list until a match is found. It also describes the binary search algorithm, which works on sorted lists by repeatedly dividing the search space in half and comparing the target element to the middle element of the sublist. The binary search algorithm narrows down the possible location using multiple comparisons, requiring fewer steps on average than the linear search for long lists.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
30 views16 pages
Chapter 3
The document discusses algorithms for searching lists of elements. It describes the linear search algorithm, which sequentially compares the target element to each element in the list until a match is found. It also describes the binary search algorithm, which works on sorted lists by repeatedly dividing the search space in half and comparing the target element to the middle element of the sublist. The binary search algorithm narrows down the possible location using multiple comparisons, requiring fewer steps on average than the linear search for long lists.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16
Discrete Mathematics
Abdelrahman Busati College of Information Technology University of Fujairah Algorithms .3 Introduction 3.1.1
Setting up the appropriate mathematical model is only part of
the solution. To complete the solution, a method is needed that will solve the general problem using the model. Ideally, what is required is a procedure that follows a sequence of steps that leads to the desired answer. Such a sequence of steps is called an algorithm. 3.1.1 Introduction EXAMPLE 1: Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers. Solution: 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. ◂ 3.1.1 Introduction • An algorithm can also be described using a computer language. However, when that is done, only those instructions permitted in the language can be used. So, instead of using a particular computer language to specify algorithms, a form of pseudocode, described in Appendix 3, will be used in this book. (We will also describe algorithms using the English language.) Pseudocode provides an intermediate step between an English-language description of an algorithm and an implementation of this algorithm in a programming language. 3.1.1 Introduction A pseudocode description of the algorithm for finding the maximum element in a finite sequence follows. 3.1.1 Introduction PROPERTIES OF ALGORITHMS There are several properties that algorithms generally share. They are useful to keep in mind when algorithms are described. These properties are: • 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. • Correctness. An algorithm should produce the correct output values for each set of input values. • 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. • Generality. The procedure should be applicable for all problems of the desired form, not just for a particular set of input values. 3.1.1 Introduction EXAMPLE 2: Show that Algorithm 1 for finding the maximum element in a finite sequence of integers has all the properties listed. • Solution: The input to Algorithm 1 is a sequence of integers. The output is the largest integer in the sequence. Each step of the algorithm is precisely defined, because only assignments, a finite loop, and conditional statements occur. To show that the algorithm is correct, we must show that when the algorithm terminates, the value of the variable max equals the maximum of the terms of the sequence. To see this, note that the initial value of max is the first term of the sequence; as successive terms of the sequence are examined, max is updated to the value of a term if the term exceeds the maximum of the terms previously examined. This (informal) argument shows that when all the terms have been examined, max equals the value of the largest term 3.1.2 Searching Algorithms The general searching problem can be described as follows: Locate an element x in a list of distinct elements a1, a2, … , an, or determine that it is not in the list. The solution to this search problem is the location of the term in the list that equals x (that is, i is the solution if x = ai ) and is 0 if x is not in the list. 3.1.2 Searching Algorithms THE LINEAR SEARCH The first algorithm that we will present is called the linear search, Links or sequential search, algorithm. The linear search algorithm begins by comparing x and a1. When x = a1, the solution is the location of a1, namely, 1. When x ≠ a1, compare x with a2. If x = a2, the solution is the location of a2, namely, 2. When x ≠ a2, compare x with a3. Continue this process, comparing x successively with each term of the list until a match is found, where the solution is the location of that term, unless no match occurs. If the entire list has been searched without locating x, the solution is 0. The pseudocode for the linear search algorithm is displayed as Algorithm 2. 3.1.2 Searching Algorithms 3.1.2 Searching Algorithms THE BINARY SEARCH We will now consider another searching algorithm. This algorithm can be used when the list has terms occurring in order of increasing size (for instance: if the terms are numbers, they are listed from smallest to largest; if they are words, they are listed Links in lexicographic, or alphabetic, order). This second searching algorithm is called the binary search algorithm. It proceeds by comparing the element to be located to the middle term of the list. The list is then split into two smaller sublists of the same size, or where one of these smaller lists has one fewer term than the other. The search continues by restricting the search to the appropriate sublist based on the comparison of the element to be located and the middle term 3.1.2 Searching Algorithms 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, • first split this list, which has 16 terms, into two smaller lists with eight terms each, namely, 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22. • Then, compare 19 and the largest term in the first list. Because 10 < 19, the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list. Next, split this list, which has eight terms, into the two smaller lists of four terms each, namely, 12 13 15 16 18 19 20 22. • Because 16 < 19 (comparing 19 with the largest term of the first list) the search is restricted to the second of these lists, which contains the 13th through the 16th terms of the original list. 3.1.2 Searching Algorithms The list 18 19 20 22 is split into two lists, namely, 18 19 20 22. • Because 19 is not greater than the largest term of the first of these two lists, which is also 19, the search is restricted to the first list: 18 19, Because 19 is not greater than the largest term of the first of these two lists, which is also 19, the search is restricted to the first list: 18 19, which contains the 13th and 14th terms of the original list. Next, this list of two terms is split into two lists of one term each: 18 and 19. Because 18 < 19, the search is restricted to the second list: the list containing the 14th term of the list, which is 19. Now that the search has been narrowed down to one term, a comparison is made, and 19 is located as the 14th term in the original list. 3.1.2 Searching Algorithms • We now specify the steps of the binary search algorithm. To search for the integer x in the list a1, a2, … , an, where a1 < a2 < ⋯ < an, begin by comparing x with the middle term am of the list, where m = ⌊(n + 1)∕2⌋. (Recall that ⌊x⌋ is the greatest integer not exceeding x.) If x > am, the search for x is restricted to the second half of the list, which is am+1, am+2, … , an. If x is not greater than am, the search for x is restricted to the first half of the list, which is a1, a2, … , am. Incomplete • The search has now been restricted to a list with no more than ⌈n∕2⌉ elements. (Recall that ⌈x⌉ is the smallest integer greater than or equal to x.) Using the same procedure, compare x to the middle term of the restricted list. Then restrict the search to the first or second half of the list. Repeat this process until a list with one term is obtained. Then determine whether this 3.1.2 Searching Algorithms