Chapter 3
Chapter 3
0 Algorithms
• Algorithm
• Pseudocode
• Computer Program
• Flow Chart
3.0 Algorithms
The term algorithm is a corruption of the name al-Khowarizmi, a
mathematician of the ninth century, whose book on Hindu numerals is the
basis of modern decimal notation. Originally, the word algorism was used for
the rules for performing arithmetic using decimal notation.
An algorithm is a finite sequence of precise instructions for performing a
computation or for solving a problem.
Describe an algorithm for finding the maximum (largest) value in a finite
sequence of integers.
Solution of Example
1: 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.
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:
Insertion Sort
Bubble Sort, Example
Insertion Sort, Example
ALGORITHM, Naive String Matcher.
The bubble sort described before Example 4 in Section 3.1 sorts a list by performing a
sequence of passes through the list. During each pass the bubble sort successively compares
adjacent elements, interchanging them if necessary. When the ith pass begins, the i − 1
largest elements are guaranteed to be in the correct positions. During this pass, n − i
comparisons are used. Consequently, the total number of comparisons used by the bubble
sort to order a list of n elements is;
What is the worst-case complexity of the insertion sort in terms of the number of
comparisons made?
Solution: The insertion sort (described in Section 3.1) inserts the jth element into the correct
position among the first j − 1 elements that have already been put into the correct order. It
does this by using a linear search technique, successively comparing the jth element with
successive terms until a term that is greater than or equal to it is found or it compares aj with
itself and stops because aj is not less than itself. Consequently, in the worst case, j
comparisons are required to insert the jth element into the correct position. Therefore, the
total number of comparisons used by the insertion sort to sort a list of n elements is;
3.3.3 Complexity of Matrix Multiplication
The definition of the product of two matrices can be expressed as an algorithm for
computing the product of two matrices. Suppose that C = [cij] is the m × n matrix that is the
product of the m × k matrix A = [aij] and the k × n matrix B = [bij]. The algorithm based on
the definition of the matrix product is expressed in pseudocode in Algorithm 1.
Solution: There are n2 entries in the product of A and B. To find each entry
requires a total of n multiplications and n − 1 additions. Hence, a total of
n3 multiplications and n2(n − 1) additions are used.
3.3.3 Complexity of Matrix Multiplication
The number of bit operations used to find the Boolean product of two n × n matrices can
be easily determined.
EXAMPLE 8 How many bit operations are used to find A ⊙ B, where A and
B are n × n zero–one matrices?
The situation is much worse for problems that cannot be solved using an
algorithm with worst-case polynomial time complexity. Such problems are
called intractable.
P versus NP problem
3.3.5 Understanding the Complexity of Algorithms