Algorithms & Flowcharts — Worked Examples

11. Working with Lists & Arrays

Scanning a list

A huge number of real programs boil down to one idea: walk through a list once, remembering something as you go. The "something you remember" might be the biggest value seen so far, the smallest, a running count, or the best two values. The pattern is always the same — start with a sensible first guess, then update it whenever you find something better. Every example below uses the sample list [4, 9, 2, 9, 7].

(a) Find the maximum (and its position)

Problem

Given a list of numbers, find the largest value and the index where it first appears.

Start best=a[0]; pos=0; i=1 i < n? a[i]>best? best=a[i]; pos=i i = i + 1 output best, pos Yes Yes No No

Pseudocode

FIND_MAX(a):
    best = a[0]
    pos  = 0
    FOR i FROM 1 TO length(a) - 1:
        IF a[i] > best:
            best = a[i]
            pos  = i
    OUTPUT best, pos

Trace table

ia[i]a[i] > best?bestpos
init——40
19Yes91
22No91
39No (not strictly greater)91
47No91

In Python

def find_max(a):
    best, pos = a[0], 0
    for i in range(1, len(a)):
        if a[i] > best:
            best, pos = a[i], i
    return best, pos

print(find_max([4, 9, 2, 9, 7]))

Output:

(9, 1)

(b) Find the minimum

Problem

Find the smallest value in a list of numbers.

Start low = a[0] , i = 1 i < n ? a[i] < low ? low = a[i] i = i + 1 output low End No Yes No Yes loop back

Pseudocode

FIND_MIN(a):
    low = a[0]
    FOR i FROM 1 TO length(a) - 1:
        IF a[i] < low:
            low = a[i]
    OUTPUT low

Trace table

ia[i]a[i] < low?low
init——4
19No4
22Yes2
39No2
47No2

In Python

def find_min(a):
    low = a[0]
    for i in range(1, len(a)):
        if a[i] < low:
            low = a[i]
    return low

print(find_min([4, 9, 2, 9, 7]))

Output:

2

(c) Count how many times a value occurs

Problem

Count how many times a target value (here, 9) appears in the list.

Start count = 0 , i = 0 i < n ? a[i] == target ? count = count + 1 i = i + 1 output count End No Yes No Yes loop back

Pseudocode

COUNT(a, target):
    count = 0
    FOR i FROM 0 TO length(a) - 1:
        IF a[i] == target:
            count = count + 1
    OUTPUT count

Trace table

Counting occurrences of 9 in [4, 9, 2, 9, 7]:

ia[i]a[i] == 9?count
init——0
04No0
19Yes1
22No1
39Yes2
47No2

In Python

def count_value(a, target):
    count = 0
    for x in a:
        if x == target:
            count += 1
    return count

print(count_value([4, 9, 2, 9, 7], 9))

Output:

2

(d) Find the second-largest value

Problem

Find the second-largest value in a list by tracking the two best values seen so far.

Start best=-inf; second=-inf more items? x > best? second=best; best=x if best>x>second: second=x output second Yes Yes No No

Pseudocode

SECOND_LARGEST(a):
    best   = -infinity
    second = -infinity
    FOR each x IN a:
        IF x > best:
            second = best
            best   = x
        ELSE IF x > second AND x < best:
            second = x
    OUTPUT second

Trace table

Scanning [4, 9, 2, 9, 7]. (Note the duplicate 9 is not greater than best, so it is skipped — the second-largest distinct value is 7.)

xx > best?bestsecond
init—-inf-inf
4Yes4-inf
9Yes94
2No (2<4)94
9No (not > best, not < best)94
7No, but 4<7<997

In Python

def second_largest(a):
    best = second = float("-inf")
    for x in a:
        if x > best:
            second, best = best, x
        elif second < x < best:
            second = x
    return second

print(second_largest([4, 9, 2, 9, 7]))

Output:

7