Scan the array once, keeping the largest value seen so far. The replay highlights when a candidate replaces the running maximum.

Algorithm

execution replay The checked-in replay follows the language-neutral state table for `array-find-max`.
cross-language comparison This Bash DSA version keeps the same data and final output as every other DSA book in this wave.

Basic Implementation

basic.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
set -euo pipefail
arr=(3 1 4 1 5 9 2 6)
best=${arr[0]}
for ((i = 1; i < ${#arr[@]}; i++)); do
	if [ "${arr[i]}" -gt "$best" ]; then
		best=${arr[i]}
	fi
done
echo "$best"
  1. arr ← [3, 1, 4, 1, 5, 9, 2, 6], best ← 3

    1#!/usr/bin/env bash2set -euo pipefail
    values this step[3, 1, 4, 1, 5, 9, 2, 6]arr3best
  2. replaced ← no

    5for ((i = 1; i < ${#arr[@]}; i++)); do6	if [ "${arr[i]}" -gt "$best" ]; then7		best=${arr[i]}
    values this stepnoreplaced1i1arr[i]3best
  3. best ← 4, replaced ← yes

    5for ((i = 1; i < ${#arr[@]}; i++)); do6	if [ "${arr[i]}" -gt "$best" ]; then7		best=${arr[i]}
    values this step3 4bestyesreplaced2i4arr[i]
  4. replaced ← no

    5for ((i = 1; i < ${#arr[@]}; i++)); do6	if [ "${arr[i]}" -gt "$best" ]; then7		best=${arr[i]}
    values this stepnoreplaced3i1arr[i]4best
  5. best ← 5, replaced ← yes

    5for ((i = 1; i < ${#arr[@]}; i++)); do6	if [ "${arr[i]}" -gt "$best" ]; then7		best=${arr[i]}
    values this step4 5bestyesreplaced4i5arr[i]
  6. best ← 9, replaced ← yes

    5for ((i = 1; i < ${#arr[@]}; i++)); do6	if [ "${arr[i]}" -gt "$best" ]; then7		best=${arr[i]}
    values this step5 9bestyesreplaced5i9arr[i]
  7. replaced ← no

    5for ((i = 1; i < ${#arr[@]}; i++)); do6	if [ "${arr[i]}" -gt "$best" ]; then7		best=${arr[i]}
    values this stepnoreplaced6i2arr[i]9best
  8. replaced ← no

    5for ((i = 1; i < ${#arr[@]}; i++)); do6	if [ "${arr[i]}" -gt "$best" ]; then7		best=${arr[i]}
    values this stepnoreplaced7i6arr[i]9best
  9. stdout ← 9

    9done10echo "$best"
    values this step9stdout9best

Complexity

  • Time: O(n)
  • Space: O(1)

Implementation notes

  • set -euo pipefail is present at the top of the script, so this lesson runs with the same strict shell settings as the rest of the Bash examples.
  • arr=(3 1 4 1 5 9 2 6) is a Bash indexed array. The indexes used here are zero-based.
  • best=${arr[0]} seeds the running maximum with 3.
  • for ((i = 1; i < ${#arr[@]}; i++)); do is Bash arithmetic-loop syntax. ${#arr[@]} is 8, so the loop scans indexes 1 through 7.
  • if [ "${arr[i]}" -gt "$best" ]; then uses the numeric comparison operator -gt. The values are quoted while Bash compares them as numbers.
  • best=${arr[i]} replaces the running maximum only when the current value is larger.

Replay steps

seed: best = 3 from arr[0]
i=1 value 1: keep 3
i=2 value 4: replace 3 -> 4
i=3 value 1: keep 4
i=4 value 5: replace 4 -> 5
i=5 value 9: replace 5 -> 9
i=6 value 2: keep 9
i=7 value 6: keep 9
  • echo "$best" prints exactly 9.