Chapter 3
With Question/Answer Animations
Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education.
Section 3.1
Problems and Algorithms
— In many domains there are key general problems that
ask for output with specific properties when given
valid input.
— The first step is to precisely state the problem, using
the appropriate structures to specify the input and the
desired output.
— We then solve the general problem by specifying the
steps of a procedure that takes a valid input and
produces the desired output. This procedure is called
an algorithm.
Algorithms Abu Ja’far Mohammed Ibin Musa Al-Khowarizmi
(780-850)
Definition: An algorithm is a finite set of precise
instructions for performing a computation or for solving a
problem.
Example: Describe an algorithm for finding the maximum
value in a finite sequence of integers.
Solution: Perform the following steps:
1. Set the temporary maximum equal to the first integer in the
sequence.
2. Compare the next integer in the sequence to the temporary
maximum.
— 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. If not, stop.
4. When the algorithm terminates, the temporary maximum is the
largest integer in the sequence.
Specifying Algorithms
— Algorithms can be specified in different ways. Their steps can be
described in English or in pseudocode.
— Pseudocode is an intermediate step between an English language
description of the steps and a coding of these steps using a
programming language.
— Programmers can use the description of an algorithm in
pseudocode to construct a program in a particular language.
— Pseudocode helps us analyze the time required to solve a
problem using an algorithm, independent of the actual
programming language used to implement algorithm.
Properties of Algorithms
— Input: An algorithm has input values from a specified set.
— Output: From the input values, the algorithm produces
the output values from a specified set. The output values
are the solution.
— Correctness: An algorithm should produce the correct
output values for each set of input values.
— Finiteness: An algorithm should produce the output after a
finite number of steps for any input.
— Effectiveness: It must be possible to perform each step of
the algorithm correctly and in a finite amount of time.
— Generality: The algorithm should work for all problems of
the desired form.
Finding the Maximum Element in a
Finite Sequence
— The algorithm in pseudocode:
procedure max(a1, a2, …., an: integers)
max := a1
for i := 2 to n
if max < ai then max := ai
return max{max is the largest element}
— Does this algorithm have all the properties listed on
the previous slide?
Some Example Algorithm Problems
— Two classes of problems will be studied in this section.
1. Searching Problems: finding the position of a
particular element in a list.
2. Sorting problems: putting the elements of a list into
increasing order.
Searching Problems
Definition: The general searching problem is to locate
an element x in the list of distinct elements a1,a2,...,an,
or determine that it is not in the list.
— The solution to a searching problem is the location of
the term in the list that equals x (that is, i is the
solution if x = ai) or 0 if x is not in the list.
— We will study two different searching algorithms; linear
search and binary search.
Linear Search Algorithm
— The linear search algorithm locates an item in a list by examining elements in
the sequence one at a time, starting at the beginning.
— First compare x with a1. If they are equal, return the position 1.
— If not, try a2. If x = a2, return the position 2.
— Keep going, and if no match is found when the entire list is scanned,
return 0.
procedure linear search(x:integer,
a1, a2, …,an: distinct integers)
i := 1
while (i ≤ n and x ≠ ai)
i := i + 1
if i ≤ n then location := i
else location := 0
return location{location is the subscript of the term that
equals x, or is 0 if x is not found}
Binary Search
— Assume the input is a list of items in increasing order.
— The algorithm begins by comparing the element to be found
with the middle element.
— If the middle element is lower, the search proceeds with the upper
half of the list.
— If it is not lower, the search proceeds with the lower half of the list
(through the middle position).
— Repeat this process until we have a list of size 1.
— If the element we are looking for is equal to the element in the list,
the position is returned.
— Otherwise, 0 is returned to indicate that the element was not
found.
— The binary search algorithm is much more efficient than linear
search.
Binary Search
— Here is a description of the binary search algorithm in
pseudocode.
procedure binary search(x: integer, a1,a2,…, an: increasing integers)
i := 1 {i is the left endpoint of interval}
j := n {j is right endpoint of interval}
while i < j
m := ⌊(i + j)/2⌋
if x > am then i := m + 1
else j := m
if x = ai then location := i
else location := 0
return location{location is the subscript i of the term ai equal to x,
or 0 if x is not found}
Binary Search
Example: The steps taken by a binary search for 19 in the list:
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
1. The list has 16 elements, so the midpoint is 8. The value in the 8th position is 10. Since
19 > 10, further search is restricted to positions 9 through 16.
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
2. The midpoint of the list (positions 9 through 16) is now the 12th position with a value
of 16. Since 19 > 16, further search is restricted to the 13th position and above.
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
3. The midpoint of the current list is now the 14th position with a value of 19. Since
19 ≯ 19, further search is restricted to the portion from the 13th through the 14th
positions .
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
4. The midpoint of the current list is now the 13th position with a value of 18.
Since 19> 18, search is restricted to the portion from the 14th position through the
14th.
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
5. Now the list has a single element and the loop ends. Since 19=19, the location 14 is
returned.
Sorting
— To sort the elements of a list is to put them in increasing
order (numerical order, alphabetic, and so on).
— Sorting is an important problem because:
— A nontrivial percentage of all computing resources are
devoted to sorting different kinds of lists, especially
applications involving large databases of information that
need to be presented in a particular order (e.g., by customer,
part number etc.).
— An amazing number of fundamentally different algorithms
have been invented for sorting. Their relative advantages and
disadvantages have been studied extensively.
— Sorting algorithms are useful to illustrate the basic notions of
computer science.
Bubble Sort
— Bubble sort makes multiple passes through a list. Every
pair of elements that are found to be out of order are
interchanged.
procedure bubblesort(a1,…,an: real numbers
with n ≥ 2)
for i := 1 to n− 1
for j := 1 to n − i
if aj >aj+1 then interchange aj and aj+1
{a1,…, an is now in increasing order}
Bubble Sort
Example: Show the steps of bubble sort with 3 2 4 1 5
— At the first pass the largest element has been put into the correct position
— At the end of the second pass, the 2nd largest element has been put into the correct
position.
— In each subsequent pass, an additional element is put in the correct position.
Insertion Sort
— Insertion sort begins with the 2nd element. It compares the 2nd
element with the 1st and puts it before the first if it is not larger.
procedure insertion sort
•Next the 3rd element is put into (a1,…,an:
the correct position among the real numbers with n ≥ 2)
first 3 elements. for j := 2 to n
•In each subsequent pass, the n+1st i := 1
element is put into its correct while aj > ai
position among the first n+1
i := i + 1
elements.
•Linear search is used to find the
m := aj
correct position. for k := 0 to j − i − 1
aj-k := aj-k-1
ai := m
{Now a1,…,an is in increasing order}
Insertion Sort
Example: Show all the steps of insertion sort with the
input: 3 2 4 1 5
i. 2 3 4 1 5 (first two positions are interchanged)
ii. 2 3 4 1 5 (third element remains in its position)
iii. 1 2 3 4 5 (fourth is placed at beginning)
iv. 1 2 3 4 5 (fifth element remains in its position)