Chapter 4.1: Algorithms

Computer Fundamentals and Introduction to Programming
ProblemAlgorithmTracingSearchingSorting
Sercan KÜLCÜ · Giresun University

Introduction

Learning Outcomes

Specify

Pin down the inputs, outputs and constraints.

Design

Break the solution into clear, finite steps.

Verify

Trace and test the algorithm on examples.

Compare

Evaluate different searching and sorting approaches.

Problem Solving

Algorithm First, Code Second

  • Code is one implementation of a solution idea, in one particular language.
  • Code written without an algorithm leads to trial and error and hidden bugs.
  • The right order: understand the problem, solve an example, write the method, then code it.
Understandlargest number?Solve an example3, 8, 5 → 8Write the methodmax = firstupdate on a larger oneCode itJava, Python, C…The orange step (the algorithm) is language-independent; the same method can be coded in any language.

Problem Solving

Problem Definition

Goal

Which problem are we solving?

Input

What data does the program receive?

Output

What result must it produce?

Constraint

What are the valid ranges and conditions?

Example

Which small example confirms the expected result?

Error case

What happens on invalid or missing input?

Input: nintegerAlgorithmcompute factorialOutput: n!5 → 120Constraint: 0 ≤ n ≤ 20 (fits in a long) · error if n < 0

Algorithm

Properties of a Good Algorithm

An algorithm is a finite sequence of well-defined steps that solves a given problem.

  • It takes zero or more inputs and produces at least one output.
  • Its steps are unambiguous, ordered and executable.
  • It halts after a finite number of steps.
  • It produces the correct result for every valid input.
  • Its resource use stays acceptable as the input grows.
i = 1; while i ≠ 10: i = i + 2never hits 10: never halts ✗01234567891011121314…i = 1; while i ≤ 10: i = i + 2halts when i = 11 ✓01234567891011121314

Algorithm

The Algorithm Design Cycle

1. Solve an example2. Find the rule3. Write pseudocode4. Trace by hand5. Fix
  • If a hand trace reveals a bug, fix it and go around again; once no bugs remain, start coding.
  • When several solutions exist, compare their correctness, readability and efficiency.

Algorithm

Pseudocode

Pseudocode describes an algorithm's logic without tying it to a programming language.

START
  read A
  read B
  SUM = A + B
  print SUM
END
STARTread Aread BSUM = A + Bprint SUMEND
  • Use short, consistent phrases close to natural language.
  • Indentation shows the scope of conditions and loops.
  • The point is clarity, not syntax.

Examples

Example: Sort Two Numbers

read a, b
if a > b then
    temp = a
    a = b
    b = temp
print a, b
  • Equal values need no swap.
  • A swap takes three assignments.
  • The output must satisfy a ≤ b.
a7 → 3temp— → 7b3 → 7① temp = a③ b = temp② a = b
Why temp? Writing a = b; b = a directly overwrites a's old value in the first assignment: for a = 7, b = 3 the result is 3 3.

Examples · Step-by-step trace

Sort Two Numbers Step by Step (a = 7, b = 3)

a7b3temp—

Read the inputs: a = 7, b = 3. Goal: print the smaller one first.

a7b3temp—

Condition: 7 > 3 → true, so we enter the swap block.

a7b3temp7copy

temp = a = 7: back up a's value first so it isn't lost.

a3b3temp7copy

a = b = 3. The old 7 now lives only in temp.

a3b7temp7copy

b = temp = 7. Three assignments, and the values have swapped.

a3b7temp7

Output 3 7: a ≤ b holds. (With a = 2, b = 9 the condition is false and lines 3–5 are skipped.)

read a, b
if a > b then
    temp = a
    a = b
    b = temp
print a, b

Examples

Example: Factorial

read n
if n < 0 then print "error" and stop
result = 1
i = 2
while i ≤ n
    result = result × i
    i = i + 1
print result
  • 0! and 1! are 1: the loop never runs and result stays 1.
  • Loop invariant: at the start of each round, result = (i − 1)!
  • On negative input, report an error and stop.
n = 5: at the start of each round, result = (i − 1)!1i = 2× 22i = 3× 36i = 4× 424i = 5× 5120i = 6 → stop

Examples · Step-by-step trace

Factorial Step by Step (n = 5)

n5result—i—product:1×2×3×4×5

Read n = 5. Expected: 5! = 1×2×3×4×5.

n5result—i—product:1×2×3×4×5

Condition: 5 < 0 → false; no error, continue.

n5result1i—product:1×2×3×4×5

A product starts at 1 (starting at 0 would make everything 0).

n5result1i2product:1×2×3×4×5

i = 2: multiplying by 1 changes nothing, so start at 2.

n5result1i2product:1×2×3×4×5

Condition: 2 ≤ 5 → true.

n5result2i2product:1×2×3×4×5

result = 1 × 2 = 2 (= 2!).

n5result2i3product:1×2×3×4×5

i = 3.

n5result2i3product:1×2×3×4×5

Condition: 3 ≤ 5 → true.

n5result6i3product:1×2×3×4×5

result = 2 × 3 = 6 (= 3!).

n5result6i4product:1×2×3×4×5

i = 4.

n5result6i4product:1×2×3×4×5

Condition: 4 ≤ 5 → true.

n5result24i4product:1×2×3×4×5

result = 6 × 4 = 24 (= 4!).

n5result24i5product:1×2×3×4×5

i = 5.

n5result24i5product:1×2×3×4×5

Condition: 5 ≤ 5 → true.

n5result120i5product:1×2×3×4×5

result = 24 × 5 = 120 (= 5!).

n5result120i6product:1×2×3×4×5

i = 6.

n5result120i6product:1×2×3×4×5

Condition: 6 ≤ 5 → false, so the loop ends.

n5result120i6product:1×2×3×4×5

Print 120. The loop ran n − 1 = 4 times.

read n
if n < 0 then print "error" and stop
result = 1
i = 2
while i ≤ n
    result = result × i
    i = i + 1
print result

Examples

Example: Fibonacci

read n
a = 0, b = 1
for round = 0 to n-1
    print a
    next = a + b
    a = b
    b = next
  • Keep the two previous values: a and b form a window that slides one step right each round.
  • The temporary variable prevents losing information during the update.
011235813abnext2 + 3 = 5Round 3: print a (2), next = a + b; then a = 3, b = 5 and the window slides one step right.

Examples · Step-by-step trace

Fibonacci Step by Step (n = 8, rounds 0–2)

output:a—b—next—

n = 8: print the first 8 Fibonacci numbers.

output:a0b1next—

The first two terms: a = 0, b = 1.

output:0a0b1next—

Round 0 (0…7): print a = 0.

output:0a0b1next1

next = 0 + 1 = 1.

output:0a1b1next1

a = b = 1: the window slides.

output:0a1b1next1

b = next = 1. Order matters: without a = b first, the old b would be lost.

output:01a1b1next1

Round 1 (0…7): print a = 1.

output:01a1b1next2

next = 1 + 1 = 2.

output:01a1b1next2

a = b = 1: the window slides.

output:01a1b2next2

b = next = 2.

output:011a1b2next2

Round 2 (0…7): print a = 1.

output:011a1b2next3

next = 1 + 2 = 3.

output:011a2b2next3

a = b = 2: the window slides.

output:011a2b3next3

b = next = 3.

read n
a = 0, b = 1
for round = 0 to n-1
    print a
    next = a + b
    a = b
    b = next

Examples · Step-by-step trace

Fibonacci Step by Step (cont.: rounds 3–5)

output:0112a2b3next3

Round 3 (0…7): print a = 2.

output:0112a2b3next5

next = 2 + 3 = 5.

output:0112a3b3next5

a = b = 3: the window slides.

output:0112a3b5next5

b = next = 5.

output:01123a3b5next5

Round 4 (0…7): print a = 3.

output:01123a3b5next8

next = 3 + 5 = 8.

output:01123a5b5next8

a = b = 5: the window slides.

output:01123a5b8next8

b = next = 8.

output:011235a5b8next8

Round 5 (0…7): print a = 5.

output:011235a5b8next13

next = 5 + 8 = 13.

output:011235a8b8next13

a = b = 8: the window slides.

output:011235a8b13next13

b = next = 13.

read n
a = 0, b = 1
for round = 0 to n-1
    print a
    next = a + b
    a = b
    b = next

Examples · Step-by-step trace

Fibonacci Step by Step (cont.: rounds 6–7)

output:0112358a8b13next13

Round 6 (0…7): print a = 8.

output:0112358a8b13next21

next = 8 + 13 = 21.

output:0112358a13b13next21

a = b = 13: the window slides.

output:0112358a13b21next21

b = next = 21.

output:011235813a13b21next21

Round 7 (0…7): print a = 13.

output:011235813a13b21next34

next = 13 + 21 = 34.

output:011235813a21b21next34

a = b = 21: the window slides.

output:011235813a21b34next34

b = next = 34.

output:011235813a21b34next34

All 8 rounds done, so the loop ends. Output: 0 1 1 2 3 5 8 13. The last term printed is 13.

read n
a = 0, b = 1
for round = 0 to n-1
    print a
    next = a + b
    a = b
    b = next

Examples

Example: Palindrome

read text
clean text by the rule
lo = 0, hi = length - 1
while lo < hi
    if the characters differ, return false
    lo = lo + 1, hi = hi - 1
return true
  • Comparison moves from both ends toward the middle.
  • Decide the case and space rules up front (e.g. "Kayak" → "kayak").
  • At most length / 2 comparisons.
0k1a2y3a4klo = 0 →← hi = 4Outside in: k = k, a = a; the middle y needs no comparison → ⌊5 / 2⌋ = 2 comparisons

Examples · Step-by-step trace

Palindrome Step by Step ("Kayak")

0K1a2y3a4k

Read the text: "Kayak". The first letter is uppercase.

0k1a2y3a4k

Rule: lowercase it and drop spaces → "kayak". Otherwise K ≠ k would be a mismatch.

0k1a2y3a4k▲ lo▲ hi

The two ends: lo = 0, hi = 4.

0k1a2y3a4k▲ lo▲ hi

Condition: 0 < 4 → true.

0k1a2y3a4k▲ lo▲ hi

text[0] = 'k', text[4] = 'k' → equal, continue.

0k1a2y3a4k▲ lo▲ hi

One step inward: lo = 1, hi = 3.

0k1a2y3a4k▲ lo▲ hi

Condition: 1 < 3 → true.

0k1a2y3a4k▲ lo▲ hi

text[1] = 'a', text[3] = 'a' → equal, continue.

0k1a2y3a4k▲ lohi

One step inward: lo = 2, hi = 2.

0k1a2y3a4k▲ lohi

Condition: 2 < 2 → false: we reached the middle, so the loop ends.

0k1a2y3a4k▲ lohi

No mismatched pair → true: "Kayak" is a palindrome. Only 2 comparisons for 5 letters.

read text
clean text by the rule
lo = 0, hi = length - 1
while lo < hi
    if the characters differ, return false
    lo = lo + 1, hi = hi - 1
return true

Examples

Example: Prime Number

read n
if n < 2 then print "not prime" and stop
i = 2
while i × i ≤ n
    if n % i == 0 then print "not prime" and stop
    i = i + 1
print "prime"
  • The search stops at the first divisor.
  • Square-root bound: if n = a × b, then a and b cannot both exceed √n; any small divisor lies in the range i × i ≤ n.
  • % gives the remainder of a division.
12346 = √36912183636 = 1×36 = 2×18 = 3×12 = 4×9 = 6×6: the smaller of each pair ≤ √36 → trying i = 2…6 is enough

Examples · Step-by-step trace

Prime Number Step by Step (n = 29)

n = 29i:234567√29 ≈ 5.39

Read n = 29.

n = 29i:234567√29 ≈ 5.39

29 < 2 → false; not 0 or 1, continue.

n = 29i:234567√29 ≈ 5.39

i = 2: the first candidate divisor.

n = 29i:234567√29 ≈ 5.39

Condition: 2×2 = 4 ≤ 29 → true.

n = 29i:229%2=134567√29 ≈ 5.39

29 % 2 = 1 → not zero, so i is not a divisor.

n = 29i:229%2=134567√29 ≈ 5.39

i = 3.

n = 29i:229%2=134567√29 ≈ 5.39

Condition: 3×3 = 9 ≤ 29 → true.

n = 29i:229%2=1329%3=24567√29 ≈ 5.39

29 % 3 = 2 → not zero, so i is not a divisor.

n = 29i:229%2=1329%3=24567√29 ≈ 5.39

i = 4.

n = 29i:229%2=1329%3=24567√29 ≈ 5.39

Condition: 4×4 = 16 ≤ 29 → true.

n = 29i:229%2=1329%3=2429%4=1567√29 ≈ 5.39

29 % 4 = 1 → not zero, so i is not a divisor.

n = 29i:229%2=1329%3=2429%4=1567√29 ≈ 5.39

i = 5.

n = 29i:229%2=1329%3=2429%4=1567√29 ≈ 5.39

Condition: 5×5 = 25 ≤ 29 → true.

n = 29i:229%2=1329%3=2429%4=1529%5=467√29 ≈ 5.39

29 % 5 = 4 → not zero, so i is not a divisor.

n = 29i:229%2=1329%3=2429%4=1529%5=467√29 ≈ 5.39

i = 6.

n = 29i:229%2=1329%3=2429%4=1529%5=467√29 ≈ 5.39

Condition: 6×6 = 36 ≤ 29 → false: i has passed √29, so there is no need to go on.

n = 29i:229%2=1329%3=2429%4=1529%5=467√29 ≈ 5.39tried 2, 3, 4, 5; 6 and beyond are unnecessary

No divisor found → prime. Only 4 divisors tried (instead of 2…28). For n = 21, i = 3 gives 21 % 3 = 0 → not prime.

read n
if n < 2 then print "not prime" and stop
i = 2
while i × i ≤ n
    if n % i == 0 then print "not prime" and stop
    i = i + 1
print "prime"

Verify

Tracing an Algorithm

  • Record each variable's value at every step in a trace table.
  • Try normal, boundary and invalid inputs separately.
  • When expected and actual results differ, find the first step where they diverge.

Trace table for factorial, n = 4 (i: value at the start of the round, result: value at the end):

Stepiresulti ≤ n
start21—
round 122true
round 236true
round 3424true
exit5245 ≤ 4 false
One passing test does not prove an algorithm correct for every input.

Verify

Test Cases: Factorial

TypeGoalInputExpected
NormalCheck typical usen = 5120
BoundaryTry the lower boundn = 01 (the loop never runs)
InvalidCheck error handlingn = -3"error"
LargeDetect overflown = 2121! = 51,090,942,171,709,440,000 → does not fit in a 64-bit integer
-3052021-3 → "error"0 → 15 → 12020! fits21! overflowsinvalid (n < 0)valid: 0 ≤ n ≤ 20long overflows
A 64-bit integer (Java long) holds at most 9,223,372,036,854,775,807; 20! = 2,432,902,008,176,640,000 fits, 21! does not. Hence the constraint 0 ≤ n ≤ 20.

Analysis

Same Problem, Two Algorithms: 1 + 2 + … + n

sum = 0
i = 1
while i <= n
    sum = sum + i
    i = i + 1

A: a loop, n rounds

sum = n * (n + 1) / 2

B: Gauss's formula, one step

Number of additionsn = 10loop: 10 additionsformula: 1 addition + 1 multiplication + 1 division · result 55n = 100loop: 100 additionsformula: 1 addition + 1 multiplication + 1 division · result 5,050n = 1,000loop: 1,000 additionsformula: 1 addition + 1 multiplication + 1 division · result 500,500
Both are correct, but as n grows A does more work while B's stays constant: efficiency matters as much as correctness.

Analysis

Correctness and Efficiency

  • Correctness means the algorithm produces the expected result.
  • Time complexity describes how the operation count grows with input size, written in big-O notation: O(n), O(log n), O(n²).
  • Space complexity measures the extra memory used.
  • On small inputs, the fastest-looking method is not always the clearest one.
n⌊log₂ n⌋ + 1 (binary search)n (linear search)n² (simple sort)
10410100
100710010,000
1,000101,0001,000,000
1,000,000201,000,0001,000,000,000,000

Searching

Linear Search

int linearSearch(int[] arr, int target) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == target) return i;
    }
    return -1;
}
  • Compares elements from first to last; the data need not be sorted.
  • Stops at the first match; returns -1 if there is none.
  • Worst case examines all n elements: O(n).

The code is in Java.

Searching · Run the code step by step

Linear Search Step by Step (target = 5)

0713293541

Call linearSearch(arr, 5). The array is not sorted, so we check one element at a time from the start.

0713293541▲ i

i = 0: 0 < 5 → true.

0713293541▲ i

arr[0] = 7 == 5 → false.

0713293541▲ i

i = 1: 1 < 5 → true.

0713293541▲ i

arr[1] = 3 == 5 → false.

0713293541▲ i

i = 2: 2 < 5 → true.

0713293541▲ i

arr[2] = 9 == 5 → false.

0713293541▲ i

i = 3: 3 < 5 → true.

0713293541▲ i

arr[3] = 5 == 5 → true → return 3. That took 4 comparisons; for target 4 it would return -1 after 5.

int linearSearch(int[] arr, int target) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == target) return i;
    }
    return -1;
}

Searching

Binary Search

int binarySearch(int[] arr, int target) {
    int lo = 0, hi = arr.length - 1;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (arr[mid] == target) return mid;
        if (arr[mid] < target) lo = mid + 1;
        else hi = mid - 1;
    }
    return -1;
}
  • Works only on sorted data; each step halves the range.
  • Worst case ⌊log₂ n⌋ + 1 rounds: O(log n).
  • lo + (hi - lo) / 2 gives the same result as (lo + hi) / 2 but does not overflow for very large indices.
  • Sorting first just to search once can raise the total cost.

Searching · Run the code step by step

Binary Search Step by Step (target = 34)

0318212317421525630734840▲ lo▲ hi

The search range is the whole array: lo = 0, hi = 8.

0318212317421525630734840▲ lo▲ hi

0 ≤ 8 → true: the range is not empty, round 1.

0318212317421525630734840▲ lo▲ hi▲ mid

mid = 0 + (8 − 0) / 2 = 4, arr[4] = 21.

0318212317421525630734840▲ lo▲ hi▲ mid

21 == 34 → false.

0318212317421525630734840▲ lo▲ hi

21 < 34 → the target is in the right half: lo = 5. The left half is gone.

0318212317421525630734840▲ lo▲ hi

5 ≤ 8 → true: the range is not empty, round 2.

0318212317421525630734840▲ lo▲ hi▲ mid

mid = 5 + (8 − 5) / 2 = 6, arr[6] = 30.

0318212317421525630734840▲ lo▲ hi▲ mid

30 == 34 → false.

0318212317421525630734840▲ lo▲ hi

30 < 34 → the target is in the right half: lo = 7. The left half is gone.

0318212317421525630734840▲ lo▲ hi

7 ≤ 8 → true: the range is not empty, round 3.

0318212317421525630734840▲ lo mid▲ hi

mid = 7 + (8 − 7) / 2 = 7, arr[7] = 34.

0318212317421525630734840▲ lo mid▲ hi

34 == 34 → return 7. Found in 3 rounds; linear search would have taken 8 comparisons.

int binarySearch(int[] arr, int target) {
    int lo = 0, hi = arr.length - 1;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (arr[mid] == target) return mid;
        if (arr[mid] < target) lo = mid + 1;
        else hi = mid - 1;
    }
    return -1;
}

Searching · Run the code step by step

Binary Search: Target Not Found (target = 19)

0318212317421525630740▲ lo▲ hi

The search range is the whole array: lo = 0, hi = 7. The target 19 is not in the array (it would sit between 17 and 21).

0318212317421525630740▲ lo▲ hi

0 ≤ 7 → true: the range is not empty, round 1.

0318212317421525630740▲ lo▲ mid▲ hi

mid = 0 + (7 − 0) / 2 = 3, arr[3] = 17.

0318212317421525630740▲ lo▲ mid▲ hi

17 == 19 → false.

0318212317421525630740▲ lo▲ hi

17 < 19 → true: the target can only be in the right half, lo = 4. The left half is gone.

0318212317421525630740▲ lo▲ hi

4 ≤ 7 → true: the range is not empty, round 2.

0318212317421525630740▲ lo▲ mid▲ hi

mid = 4 + (7 − 4) / 2 = 5, arr[5] = 25.

0318212317421525630740▲ lo▲ mid▲ hi

25 == 19 → false.

0318212317421525630740▲ lo hi

25 < 19 → false, else: the target can only be in the left half, hi = 4. The right half is gone.

0318212317421525630740▲ lo hi

4 ≤ 4 → true: the range is not empty, round 3.

0318212317421525630740▲ lo mid hi

mid = 4 + (4 − 4) / 2 = 4, arr[4] = 21.

0318212317421525630740▲ lo mid hi

21 == 19 → false.

0318212317421525630740▲ hi▲ lo

21 < 19 → false, else: the target can only be in the left half, hi = 3. The right half is gone.

0318212317421525630740▲ hi▲ lo

4 ≤ 3 → false: lo has passed hi; the range is empty, so the loop ends.

0318212317421525630740▲ hi▲ lo

return -1: target not found. The range emptied in 3 rounds, without checking all 8 elements.

int binarySearch(int[] arr, int target) {
    int lo = 0, hi = arr.length - 1;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (arr[mid] == target) return mid;
        if (arr[mid] < target) lo = mid + 1;
        else hi = mid - 1;
    }
    return -1;
}

Searching

Comparing the Searches

PropertyLinearBinary
Sorted dataNot neededRequired
Worst-case timeO(n)O(log n)
StructureArray or streamSorted, random-access structure
UseSmall or unsorted dataMany searches
Linear12345678910111213141516target is the last element: all 16 are checked → 16 comparisonsBinary12345the range halves at each step: 16 → 8 → 4 → 2 → 1 ⇒ 5 comparisons

Searching

Searching: How Many Comparisons in the Worst Case?

016324864016324864array size nworst-case comparisonslinear (64)binary (7)

The values were counted by actually running both algorithms with a missing target. For n = 1,000,000, linear search needs 1,000,000 comparisons; binary search only 20 rounds.

Sorting

Selection Sort

  • Find the smallest element in the unsorted part.
  • Swap it into the first position of that part.
  • Always n(n−1)/2 comparisons → O(n²); at most n − 1 swaps; O(1) extra memory.
for i = 0 to n-2
    minIdx = i
    for j = i+1 to n-1
        if arr[j] < arr[minIdx] then minIdx = j
    swap arr[i] and arr[minIdx]
i = 02910143713iminIdxswapafter1029143713sorted | unsortedsmallest (10) found

Sorting · Step-by-step trace

Selection Sort Step by Step

029110214337413▲ iminIdx

i = 0: the unsorted part is 0…4. Candidate minimum: arr[0] = 29.

029110214337413▲ i▲ jminIdx

arr[1] = 10 < arr[0] = 29 → true → minIdx = 1.

029110214337413▲ i▲ j▲ minIdx

arr[2] = 14 < arr[1] = 10 → false.

029110214337413▲ i▲ j▲ minIdx

arr[3] = 37 < arr[1] = 10 → false.

029110214337413▲ i▲ j▲ minIdx

arr[4] = 13 < arr[1] = 10 → false.

010129214337413▲ i▲ minIdx

Swap arr[0] and arr[1] → 10 is in place.

010129214337413▲ iminIdx

i = 1: the unsorted part is 1…4. Candidate minimum: arr[1] = 29.

010129214337413▲ i▲ jminIdx

arr[2] = 14 < arr[1] = 29 → true → minIdx = 2.

010129214337413▲ i▲ j▲ minIdx

arr[3] = 37 < arr[2] = 14 → false.

010129214337413▲ i▲ jminIdx

arr[4] = 13 < arr[2] = 14 → true → minIdx = 4.

010113214337429▲ i▲ minIdx

Swap arr[1] and arr[4] → 13 is in place.

010113214337429▲ iminIdx

i = 2: the unsorted part is 2…4. Candidate minimum: arr[2] = 14.

010113214337429▲ iminIdx▲ j

arr[3] = 37 < arr[2] = 14 → false.

010113214337429▲ iminIdx▲ j

arr[4] = 29 < arr[2] = 14 → false.

010113214337429▲ iminIdx

The minimum is already in place (minIdx = i), so the swap has no effect.

010113214337429▲ iminIdx

i = 3: the unsorted part is 3…4. Candidate minimum: arr[3] = 37.

010113214337429▲ i▲ jminIdx

arr[4] = 29 < arr[3] = 37 → true → minIdx = 4.

010113214329437▲ i▲ minIdx

Swap arr[3] and arr[4] → 29 is in place. Sorted, with 10 comparisons = n(n−1)/2.

for i = 0 to n-2
    minIdx = i
    for j = i+1 to n-1
        if arr[j] < arr[minIdx] then minIdx = j
    swap arr[i] and arr[minIdx]

Sorting

Bubble Sort

  • Compares adjacent elements and swaps them if they are out of order.
  • Each pass carries the largest remaining element to the end; the next pass is one element shorter.
  • If a pass makes no swaps, the array is sorted and we stop.
  • O(n²) in general; best case (already sorted) is a single pass: O(n).
round = 0
repeat
    swapped = false
    for j = 0 to n-2-round
        if arr[j] > arr[j+1] then
            swap arr[j] and arr[j+1]
            swapped = true
    round = round + 1
until swapped is false

Sorting · Step-by-step trace

Bubble Sort Step by Step (pass 1)

0511243248

round = 0. Adjacent pairs are compared from left to right.

0511243248

Pass 1: swapped = false; j = 0…3.

0511243248▲ j▲ j+1

arr[0] = 5 > arr[1] = 1 → true → swap.

0115243248▲ j▲ j+1

Swapped: 5 moved one step right; swapped = true.

0115243248▲ j▲ j+1

arr[1] = 5 > arr[2] = 4 → true → swap.

0114253248▲ j▲ j+1

Swapped: 5 moved one step right; swapped = true.

0114253248▲ j▲ j+1

arr[2] = 5 > arr[3] = 2 → true → swap.

0114223548▲ j▲ j+1

Swapped: 5 moved one step right; swapped = true.

0114223548▲ j▲ j+1

arr[3] = 5 > arr[4] = 8 → false, already in order.

0114223548

Pass done: 8 is in its final place (green border). round = 1.

0114223548

swapped = true → another pass is needed.

round = 0
repeat
    swapped = false
    for j = 0 to n-2-round
        if arr[j] > arr[j+1] then
            swap arr[j] and arr[j+1]
            swapped = true
    round = round + 1
until swapped is false

Sorting · Step-by-step trace

Bubble Sort Step by Step (cont.: pass 2)

0114223548

Pass 2: swapped = false; j = 0…2.

0114223548▲ j▲ j+1

arr[0] = 1 > arr[1] = 4 → false, already in order.

0114223548▲ j▲ j+1

arr[1] = 4 > arr[2] = 2 → true → swap.

0112243548▲ j▲ j+1

Swapped: 4 moved one step right; swapped = true.

0112243548▲ j▲ j+1

arr[2] = 4 > arr[3] = 5 → false, already in order.

0112243548

Pass done: 5 is in its final place (green border). round = 2.

0112243548

swapped = true → another pass is needed.

round = 0
repeat
    swapped = false
    for j = 0 to n-2-round
        if arr[j] > arr[j+1] then
            swap arr[j] and arr[j+1]
            swapped = true
    round = round + 1
until swapped is false

Sorting · Step-by-step trace

Bubble Sort Step by Step (cont.: pass 3)

0112243548

Pass 3: swapped = false; j = 0…1.

0112243548▲ j▲ j+1

arr[0] = 1 > arr[1] = 2 → false, already in order.

0112243548▲ j▲ j+1

arr[1] = 2 > arr[2] = 4 → false, already in order.

0112243548

Pass done: 4 is in its final place (green border). round = 3.

0112243548

swapped = false → no swaps at all, so the array is sorted; stop. 9 comparisons in total.

round = 0
repeat
    swapped = false
    for j = 0 to n-2-round
        if arr[j] > arr[j+1] then
            swap arr[j] and arr[j+1]
            swapped = true
    round = round + 1
until swapped is false

Sorting

Insertion Sort

  • Like arranging playing cards in your hand: the left part is always sorted.
  • Take the next element (the key), shift larger ones one step right, and drop the key into the gap.
  • Worst case (reverse order) O(n²); on a sorted array each key needs one comparison: O(n). O(1) extra memory.
  • Very fast on small or nearly sorted data.
for i = 1 to n-1
    key = arr[i]
    j = i - 1
    while j >= 0 and arr[j] > key
        arr[j+1] = arr[j]
        j = j - 1
    arr[j+1] = key
sorted partkey245323455 > 3 and 4 > 3: shift each right2 > 3 is false: stop3 drops into the gap

Sorting · Step-by-step trace

Insertion Sort Step by Step

0412531key3

i = 1: take key = arr[1] = 3, leaving a gap. The left part (0…0) is sorted. j = 0.

0412531key3

j = 0 ≥ 0 and arr[0] = 4 > 3 → true.

0142531key3shift right

4 moved one step right; the gap is now cell 0. j = -1.

0142531key3

j = -1 < 0 → false (we hit the left end), so the loop ends.

03142531

arr[0] = 3: the key drops into the gap. Now 0…1 is sorted.

0314231key5

i = 2: take key = arr[2] = 5, leaving a gap. The left part (0…1) is sorted. j = 1.

0314231key5

j = 1 ≥ 0 and arr[1] = 4 > 5 → false, so the loop ends.

03142531

arr[2] = 5: the key drops into the gap. Now 0…2 is sorted.

0314253key1

i = 3: take key = arr[3] = 1, leaving a gap. The left part (0…2) is sorted. j = 2.

0314253key1

j = 2 ≥ 0 and arr[2] = 5 > 1 → true.

0314235key1shift right

5 moved one step right; the gap is now cell 2. j = 1.

0314235key1

j = 1 ≥ 0 and arr[1] = 4 > 1 → true.

0312435key1shift right

4 moved one step right; the gap is now cell 1. j = 0.

0312435key1

j = 0 ≥ 0 and arr[0] = 3 > 1 → true.

0132435key1shift right

3 moved one step right; the gap is now cell 0. j = -1.

0132435key1

j = -1 < 0 → false (we hit the left end), so the loop ends.

01132435

arr[0] = 1: the key drops into the gap. Now 0…3 is sorted. Done, with 5 comparisons.

for i = 1 to n-1
    key = arr[i]
    j = i - 1
    while j >= 0 and arr[j] > key
        arr[j+1] = arr[j]
        j = j - 1
    arr[j+1] = key

Sorting

Comparing the Sorts

AlgorithmKey ideaWorstBestExtra space
SelectionSelect the smallest and place itO(n²)O(n²)O(1)
BubbleSwap neighborsO(n²)O(n) (with early exit)O(1)
InsertionInsert into the sorted partO(n²)O(n)O(1)
MergeSplit, sort, mergeO(n log n)O(n log n)O(n)

Sorting

Sorting: How Many Comparisons?

0600120018000102030405060array size naverage comparisonsSelection (1770)Bubble (1739)Insertion (955)Merge (281)

For each n, 20 random arrays were actually sorted by all four algorithms and the averages plotted. At n = 60, selection sort made 1770 comparisons; merge sort only 281.

Summary

Chapter Summary

  • An algorithm is a clear, finite set of steps that turns input into output.
  • Pseudocode writes an algorithm without committing to a language.
  • Hand tracing and a variety of test cases help assess correctness.
  • Linear search needs no sorting; binary search halves the range on sorted data.
  • Besides correctness, algorithms are compared by time and memory cost.

Summary

Review Questions

  1. What belongs in a good problem definition?
  2. Why write pseudocode before choosing a programming language?
  3. How does the factorial algorithm handle an input of 0?
  4. Why does binary search need sorted data?
  5. What does comparing O(n) with O(log n) tell us?
  6. What is the key difference between selection sort and bubble sort?

Summary

Exercises

  1. Write pseudocode that prints three numbers (a, b, c) in ascending order; build a trace table for a = 9, b = 2, c = 5.
  2. Write an algorithm that sums the even numbers from 1 to n. Trace it for n = 0 and n = 7.
  3. Change the factorial algorithm to warn when n = 21 is entered (constraint 0 ≤ n ≤ 20).
  4. Sort [6, 2, 8, 3] by hand with selection, bubble and insertion sort; count the comparisons for each.
  5. Use binary search to look for 27 and 4 in [2, 5, 9, 14, 20, 27, 33]; write down lo, hi and mid in every round.
  6. Write an algorithm that sums the digits of a number (e.g. 472 → 13) using % and /.
Hints
  1. Apply the sort-two-numbers step three times (a-b, b-c, a-b).
  2. Start at i = 2 and step by i = i + 2; n = 7 → 12.
  3. Binary search: 27 → 2 rounds; 4 → 3 rounds, then -1.

Summary

References

  • Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022.
  • Levitin, Introduction to the Design and Analysis of Algorithms, 3rd ed., Pearson, 2012.
  • Sedgewick and Wayne, Algorithms, 4th ed., Addison-Wesley, 2011 (book site).
  • Sedgewick and Wayne, Computer Science: An Interdisciplinary Approach, Addison-Wesley, 2016: 4.1 Performance, 4.2 Sorting and Searching.
  • Oracle, Java SE 21 API: java.util.Arrays (sort, binarySearch).

The End

Chapter 4.1: Algorithms
Previous: Chapter 3.3: Microsoft PowerPoint · Next: Chapter 4.2: Flowcharts
1 / 1Sercan KÜLCÜ. All rights reserved.