Chapter 1.3: Algorithms
Introduction
Learning Outcomes
Define a problem by its input, output and test cases; write the solution first as an algorithm, then as code.
Express an algorithm in pseudocode and trace it by hand, step by step, with a variable table.
Turn input, output, decision and loop steps into their Java equivalents.
Write and test algorithms for printing in order, factorial, Fibonacci, palindromes, primes and powers.
Compare random, linear and binary search; explain why binary search needs sorted data.
Apply shuffling, selection sort and bubble sort to an array step by step.
Problem Solving
Algorithm First, Then Code
- Step 1: A clear problem definition is the key to success: what is given, what is wanted?
- Step 2: The algorithm gives step-by-step directions for solving the problem. Choosing the right one makes the program efficient.
- Step 3: With the algorithm settled, we code it in the chosen language (Java, for us).
- Step 4: Test cases verify the program and expose bugs; go back to earlier steps when needed.
Problem Solving
What Is an Algorithm?
Algorithm: a finite, ordered sequence of clear steps that solves a problem.
- The word comes from the Latin spelling of the name of the 9th-century mathematician al-Khwarizmi.
- A computer does not understand algorithms; it runs one once we translate it into a programming language.
- Several algorithms may solve the same problem; we compare them by speed and memory.
Zero or more initial values.
At least one result.
Every step is clear and unambiguous.
It ends after finitely many steps.
Every step can actually be carried out.
Problem Solving
Defining the Problem
- Describe the program's input and output in plain language.
- Write test cases to check the program works: for each input, write down the expected output in advance.
Problem: add two numbers
| Test | Input | Expected output |
|---|---|---|
| 1 | 3, 5 | 8 |
| 2 | −2, 7 | 5 |
| 3 (edge) | 0, 0 | 0 |
Problem: count the words in a text
| Test | Input | Expected output |
|---|---|---|
| 1 | "Hello, world!" | 2 |
| 2 | "Just a test." | 3 |
| 3 (edge) | "" (empty text) | 0 |
The third rows are edge cases: extreme inputs like zero, empty text or negative numbers are where bugs most often hide.
Problem Solving
Designing and Testing the Algorithm
Lay out, in order, the steps that solve the problem logically.
Problem: add two numbers.
Algorithm:
- Get the first number.
- Get the second number.
- Add the two numbers.
- Print the sum.
Once the algorithm exists, the next step is testing:
- Pick small but meaningful test cases.
- Run the algorithm on paper step by step, recording each result in a variable table (a dry run).
Dry run of test 1 (input 3, 5): it prints 8, as expected
| Step | num1 | num2 | sum | Screen |
|---|---|---|---|---|
| 1. Get the first number | 3 | |||
| 2. Get the second number | 3 | 5 | ||
| 3. Add the two numbers | 3 | 5 | 8 | |
| 4. Print the sum | 3 | 5 | 8 | 8 |
Problem Solving
Expressing an Algorithm: Pseudocode
An algorithm can be written in plain language, in pseudocode, as a flowchart or directly in a programming language. Pseudocode looks like code but has no compiler rules; it is written for people, and it helps catch logic errors before coding and makes teamwork easier.
| Pseudocode | Meaning | Java |
|---|---|---|
Start … End | Start and end of the algorithm | the { … } body of main |
Read x | Get a value from the user | x = scanner.nextInt(); |
Print "…", x | Print to the screen | System.out.println("…" + x); |
x = 5 | Assign a value to a variable | x = 5; |
If x = y then | Test for equality | if (x == y) (two equals signs!) |
Else | If the condition is false | else |
For i = 1 to n | Repeat | for (int i = 1; i <= n; i++) |
Break | Leave the loop at once | break; |
%, and, or | Remainder, logical and/or | %, &&, || |
Implementation
Implementation
- After the algorithm comes coding.
- A programming language expresses the algorithm in a form the computer can compile and run.
- To keep a program manageable and readable, split the code into logical parts: input, computation, output; in larger programs, methods.
- Each pseudocode line usually maps to one or a few lines of Java.
| Algorithm step | Java |
|---|---|
| Get the first number | num1 = scanner.nextDouble(); |
| Get the second number | num2 = scanner.nextDouble(); |
| Add the two numbers | sum = num1 + num2; |
| Print the sum | System.out.println(sum); |
The point is to see how each algorithm step maps to a line of code. Choose short, descriptive names for your own variables.
Implementation · Code trace
Tracing the Addition Program
A Scanner is created to read the keyboard; no numbers yet.
print writes the prompt without a newline; the cursor waits right after it.
The user types 3 and presses Enter; nextDouble() reads it as 3.0 into num1.
The second prompt is printed.
The user enters 5; num2 = 5.0.
The right side is evaluated first: 3.0 + 5.0 = 8.0; the result goes into sum.
+ joins strings; the numbers are converted to text to form one line.
The scanner is closed and the program ends. The output matches the expected 8.
Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); double num1 = scanner.nextDouble(); System.out.print("Enter the second number: "); double num2 = scanner.nextDouble(); double sum = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + sum); scanner.close();
Sample Algorithms
Sample Algorithms
For each algorithm: problem definition → pseudocode → dry run with a variable table → Java code and tests.
Decision: If … Else
Nested decisions, and
A loop and an accumulator
Shifting variables in a loop
Clean, reverse, compare
A flag and breaking out of a loop
Repeated multiplication
In the second half: random, linear and binary search; shuffle, selection sort, bubble sort.
Sample Algorithms
Printing Two Integers in Ascending Order
Start Print "Enter the first integer: " Read firstNum Print "Enter the second integer: " Read secondNum If firstNum < secondNum then Print firstNum, " ", secondNum Else Print secondNum, " ", firstNum End If End
Problem definition
Input: two integers. Output: the same numbers, smaller first.
| Input | Expected |
|---|---|
| 4, 11 | 4 11 |
| 13, 11 | 11 13 |
| 7, 7 | 7 7 |
If the numbers are equal the condition is false and the Else branch runs; since both are the same, the output is still right.
Sample Algorithms · Dry run
Two Integers: Dry Run with (4, 11) and (13, 11)
The user is asked for the first number.
The user enters 4; it goes into the firstNum box.
The second number is requested; the user enters 11.
Condition: 4 < 11 → true.
The condition is true, so the then branch runs: first, then second.
The algorithm ends. Output 4 11, as expected.
Second test: the algorithm runs again with 13 and 11.
The user enters 13; it goes into the firstNum box.
The second number is requested; the user enters 11.
Condition: 13 < 11 → false.
False: we move to the Else branch.
The second (smaller) prints first, then the first.
The algorithm ends. Output 11 13, as expected.
Start Print "Enter the first integer: " Read firstNum Print "Enter the second integer: " Read secondNum If firstNum < secondNum then Print firstNum, " ", secondNum Else Print secondNum, " ", firstNum End If End
Sample Algorithms
Printing Three Integers in Ascending Order
Start // Get the first integer Print "Enter the first integer: " Read firstNum // Get the second integer Print "Enter the second integer: " Read secondNum // Get the third integer Print "Enter the third integer: " Read thirdNum // Print in ascending order If firstNum ≤ secondNum and firstNum ≤ thirdNum then Print firstNum If secondNum ≤ thirdNum then Print secondNum, " ", thirdNum Else Print thirdNum, " ", secondNum End If
Else If secondNum ≤ firstNum and secondNum ≤ thirdNum then Print secondNum If firstNum ≤ thirdNum then Print firstNum, " ", thirdNum Else Print thirdNum, " ", firstNum End If Else Print thirdNum If firstNum ≤ secondNum then Print firstNum, " ", secondNum Else Print secondNum, " ", firstNum End If End If End
Idea: find and print the smallest first, then order the other two with the two-integer algorithm. The same small algorithm appears three times: a natural candidate for a method.
Sample Algorithms
Three Integers: Java Code and Tests
import java.util.Scanner; public class SortThree { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter three integers: "); int first = scanner.nextInt(); int second = scanner.nextInt(); int third = scanner.nextInt(); if (first <= second && first <= third) { System.out.print(first + " "); if (second <= third) System.out.println(second + " " + third); else System.out.println(third + " " + second); } else if (second <= first && second <= third) { System.out.print(second + " "); if (first <= third) System.out.println(first + " " + third); else System.out.println(third + " " + first); } else { System.out.print(third + " "); if (first <= second) System.out.println(first + " " + second); else System.out.println(second + " " + first); } scanner.close(); } }
Tests: all 3! = 6 orders (1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1) → 1 2 3; equal values 5 5 2 → 2 5 5.
Sample Algorithms · Code trace
Three Integers: Tracing the Java Code (7, 3, 5)
Input 7, 3, 5. First condition: 7 ≤ 3 is false; the right side of && is not even evaluated.
Second condition: 3 ≤ 7 is true and 3 ≤ 5 is true → the smallest is second.
The smallest is printed; print adds no newline.
The other two are compared: 7 ≤ 5 is false.
The else branch prints 5, then 7. Output 3 5 7.
if (first <= second && first <= third) { System.out.print(first + " "); if (second <= third) System.out.println(second + " " + third); else System.out.println(third + " " + second); } else if (second <= first && second <= third) { System.out.print(second + " "); if (first <= third) System.out.println(first + " " + third); else System.out.println(third + " " + first); } else { System.out.print(third + " "); if (first <= second) System.out.println(first + " " + second); else System.out.println(second + " " + first); }
Sample Algorithms
Computing a Factorial
Start // Enter a number Read num // Initialize the result factorial = 1 // Loop to compute the factorial For i = 1 to num factorial = factorial * i End For Print "Factorial of the number: ", factorial End
Definition: n! = 1 × 2 × 3 × … × n, and 0! = 1.
| n | n! |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 3 | 6 |
| 5 | 120 |
| 10 | 3,628,800 |
factorial is an accumulator: it starts at 1, the identity for multiplication. Starting at 0 would make the result always 0.
Sample Algorithms · Dry run
Factorial: Dry Run (num = 5)
The user enters 5.
The accumulator starts at 1.
Loop: i = 1 (i ≤ 5, so the body runs).
factorial = 1 × 1 = 1.
Loop: i = 2 (i ≤ 5, so the body runs).
factorial = 1 × 2 = 2.
Loop: i = 3 (i ≤ 5, so the body runs).
factorial = 2 × 3 = 6.
Loop: i = 4 (i ≤ 5, so the body runs).
factorial = 6 × 4 = 24.
Loop: i = 5 (i ≤ 5, so the body runs).
factorial = 24 × 5 = 120.
i becomes 6; 6 ≤ 5 is false, so the loop ends.
The result is printed: 5! = 120.
Start Read num factorial = 1 For i = 1 to num factorial = factorial * i End For Print "Factorial of the number: ", factorial End
Sample Algorithms
The Fibonacci Sequence
Start // Enter the number of terms Read n // Initialize the first two terms a = 0 b = 1 If n ≥ 1 then Print a If n ≥ 2 then Print b // Compute the remaining terms For i = 2 to n − 1 result = a + b a = b b = result Print result End For End
Definition: the first two terms are 0 and 1; each later term is the sum of the previous two.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
| n (number of terms) | Expected output |
|---|---|
| 1 | 0 |
| 2 | 0 1 |
| 5 | 0 1 1 2 3 |
| 7 | 0 1 1 2 3 5 8 |
The loop runs n − 2 times, for i = 2 … n − 1; the first two terms print before it. The lines a = b and b = result "shift" the values one step.
Sample Algorithms · Dry run
Fibonacci: Dry Run (n = 5)
The user enters n = 5: we want the first 5 terms.
The first two terms: a = 0, b = 1.
5 ≥ 1: print a.
5 ≥ 2: print b.
Loop: i = 2 (i ≤ n − 1 = 4).
result = a + b = 0 + 1 = 1.
a = b → a = 1 (shifted one step).
b = result → b = 1.
The new term is printed: 1.
Loop: i = 3 (i ≤ n − 1 = 4).
result = a + b = 1 + 1 = 2.
a = b → a = 1 (shifted one step).
b = result → b = 2.
The new term is printed: 2.
Loop: i = 4 (i ≤ n − 1 = 4).
result = a + b = 1 + 2 = 3.
a = b → a = 2 (shifted one step).
b = result → b = 3.
The new term is printed: 3.
i becomes 5; 5 ≤ 4 is false, the loop ends. Exactly 5 terms printed: 0 1 1 2 3.
Start Read n a = 0 b = 1 If n ≥ 1 then Print a If n ≥ 2 then Print b For i = 2 to n − 1 result = a + b a = b b = result Print result End For End
Sample Algorithms
Checking for a Palindrome
Start // Ask for a word or sentence Read inputText // Clean: lowercase, no spaces or punctuation cleanText = Clean(inputText) // Reverse the cleaned input reversed = Reverse(cleanText) // Compare cleaned and reversed input If cleanText = reversed then Print "The text is a palindrome." Else Print "The text is not a palindrome." End If End
Palindrome: a word or sentence that reads the same backwards: kayak, abcba, "Ey Edip Adana'da pide ye". Case, spaces and punctuation are ignored, so the text is cleaned before comparing.
| Input | Expected |
|---|---|
| abcba | palindrome |
| Kayak | palindrome |
| Kitap | not |
Sample Algorithms · Dry run
Palindrome: Dry Run (abcba and Kitap)
The user enters abcba.
Cleaning: convert to lowercase (no change).
The clean text read backwards: abcba.
Compare letter by letter: all letters match → true.
True: the palindrome message prints.
Second test: the user enters Kitap.
Cleaning: convert to lowercase ("Kitap" → "kitap").
The clean text read backwards: patik.
Compare letter by letter: kitap ≠ patik → false.
False: the Else branch runs.
Start Read inputText cleanText = Clean(inputText) reversed = Reverse(cleanText) If cleanText = reversed then Print "The text is a palindrome." Else Print "The text is not a palindrome." End If End
Sample Algorithms
Checking for a Prime
Start // Enter a number Read num // Flag: 0 and 1 are not prime If num < 2 then isPrime = False Else isPrime = True // Try divisors from 2 to half the number For divisor = 2 to num / 2 If num % divisor = 0 then // If divisible, not prime isPrime = False Break End If End For // Print the result based on the flag If isPrime = True then Print num, " is a prime number." Else Print num, " is not a prime number." End If End
Prime: a number greater than 1 divisible only by 1 and itself: 2, 3, 5, 7, 11, …
- Flag: assume "prime" at first; lower the flag when a divisor turns up.
%gives the remainder: 21 % 3 = 0 → divisible.- Once a divisor is found there is no need to go on: Break.
- No number above half of n (other than n) can divide it;
num / 2is integer division (7 / 2 = 3). - For 0 and 1 the loop never runs, so they are checked separately.
Sample Algorithms · Dry run
Prime: Dry Run (7 and 21)
The user enters 7.
7 ≥ 2, so the flag starts as True.
Loop: divisor = 2 (≤ 7 / 2 = 3).
7 % 2 = 1 ≠ 0 → not divisible, go on.
Loop: divisor = 3 (≤ 7 / 2 = 3).
7 % 3 = 1 ≠ 0 → not divisible, go on.
divisor becomes 4; 4 ≤ 3 is false, the loop ends. No divisor found.
The flag is True: 7 is prime.
Second test: the user enters 21.
21 ≥ 2, so the flag starts as True.
Loop: divisor = 2 (≤ 21 / 2 = 10).
21 % 2 = 1 ≠ 0 → not divisible, go on.
Loop: divisor = 3 (≤ 21 / 2 = 10).
21 % 3 = 0 → divisible!
The flag is lowered: isPrime = False.
Break: the remaining divisors are skipped.
The flag is False: the Else branch runs.
Start Read num If num < 2 then isPrime = False Else isPrime = True For divisor = 2 to num / 2 If num % divisor = 0 then isPrime = False Break End If End For If isPrime = True then Print num, " is a prime number." Else Print num, " is not a prime number." End If End
Sample Algorithms
Computing a Power
Start // Enter a number Read base // Enter an exponent Read exp // Initialize the result result = 1 // Compute the result using the exponent For i = 1 to exp result = result * base End For Print "Result: ", result End
Definition: baseexp = base × base × … × base (exp times); base0 = 1.
| base | exp | Expected |
|---|---|---|
| 3 | 4 | 81 |
| 2 | 10 | 1024 |
| 5 | 0 | 1 |
The same pattern as factorial: the accumulator starts at 1 and each round does one multiplication. The difference: it always multiplies by the same number (the base). With exponent 0 the loop never runs and the result stays 1.
Sample Algorithms · Dry run
Powers: Dry Run (34)
The user enters base = 3.
The user enters exp = 4: we will compute 34.
The accumulator starts at 1.
Loop: i = 1.
result = 1 × 3 = 3.
Loop: i = 2.
result = 3 × 3 = 9.
Loop: i = 3.
result = 9 × 3 = 27.
Loop: i = 4.
result = 27 × 3 = 81.
i = 5 > 4: the loop ends.
The result is printed: 34 = 81.
Start Read base Read exp result = 1 For i = 1 to exp result = result * base End For Print "Result: ", result End
Searching
Search Algorithms
- Asks whether a given item is in a collection (and if so, where).
- A basic operation for processing data and retrieving information.
- It helps find specific items quickly and correctly.
- Uses: finding a book in a library, a page on a website, a record in a database, a name in a phone book.
Input:
- A collection of objects, say a "Basket".
- A particular object, say an "Apple".
Output:
trueif the "Apple" is in the basket.falseif it is not.
In code we usually return the index (position) of the item, or -1 if it is missing.
Searching
Three Search Methods
Each step picks a random item and compares it with the target.
No order needed · no guarantee of finding it
Compares the items one by one from start to end.
No order needed · at most n comparisons
Looks at the middle item and discards half the range each step.
Array must be sorted · at most ≈ log₂ n comparisons
Searching
Random Search
- Checks items at random: each step picks a random item.
- Runs until it finds the target or runs out of tries.
- Pseudocode:
- Pick a random item.
- Compare it with the target.
- If it is the target, stop.
- Otherwise repeat.
Pros
- Simple.
- The order of the data does not matter.
Cons
- Not always effective: it may pick the same item again and again.
- It can raise the odds of finding the item but guarantees nothing.
- If the target is absent it runs forever; it needs a limit on tries.
- In the worst case it scans the whole collection (or more).
Searching · Step by step
Random Search: Step by Step
We have an unsorted array; the target is 48.
Step 1: the random pick is 23; 23 ≠ 48, not found yet. Pick another.
Step 2: the random pick is 42; 42 ≠ 48, not found yet. Pick another.
Step 3: the random pick is 7; 7 ≠ 48, not found yet. Pick another.
Step 4: the random pick is 48 → target found; the search ends (4 tries).
Searching
Linear Search
- The most basic way to find an item in a collection.
- It checks the items in order looking for the target.
- The items need not be sorted.
- Good for small collections.
Steps:
- Put the items in a list (array).
- Starting from the first, compare each item with the target in turn.
- If it is the target, stop and return its position.
- Otherwise move to the next item.
- Continue to the end of the list; reaching the end means the item is absent.
Pros
- Simple and easy to follow.
- Works in any order (on unsorted data).
- The result is certain: if the item is there, it is found.
Cons
- Slow on large collections: it may have to check every item (worst case n comparisons).
Searching · Step by step
Linear Search: Step by Step
Step 1: the target is 23. Start with the first item (42) and compare it with the target.
Step 2: the first item (42) does not match 23. Move on: 17.
Step 3: the second item (17) does not match either. Next: 8.
Step 4: the third item (8) does not match either. Next: 23.
Step 5: the fourth item (23) matches; the search ends here (4 comparisons).
Searching
Binary Search
- A fast way to find an item in a collection.
- Works on sorted data (ascending or descending).
- Each step discards half of the list.
- Ideal for searching large collections.
Steps:
- Take the middle item and compare it with the target.
- If it is the target, stop.
- If the target is smaller, focus on the left half.
- If the target is larger, focus on the right half.
- Repeat until the target is found or the range is empty.
Pros
- Fast: at most 20 comparisons for 1,000,000 items.
- Very fast on large collections.
Cons
- The data must be sorted (and sorting has a cost).
- On an unsorted array it can give a wrong answer (without any error!).
Searching · Step by step
Binary Search: Step by Step
We have a sorted array; the target is 48. At first the range is the whole array: low = 0, high = 8.
Step 1: take the middle item (42) and compare it with the target (48).
Step 2: 48 is greater than the middle item (42), so the target is in the right half. The left half is dropped: [48, 55, 64, 77].
Step 3: take the middle of the right half. With 4 items there are two middles (55 and 64); (low + high) / 2 picks the left one, 55.
Step 4: 48 is less than the middle item (55); the right side is dropped. Only [48] remains.
Step 5: the middle item, 48, equals the target: found. Just 3 comparisons in a 9-item array.
Searching
Binary vs. Linear Search
- Checks the items one by one, in order.
- Runs until it finds the target or the list ends.
- Data may be unsorted.
- Can be slow on large collections.
- Finds the target quickly in sorted data.
- Data must be sorted (ascending or descending).
- Each step discards half of what remains.
- Very fast on large collections.
Comparisons in the worst case
| Items n | Linear (n) | Binary (⌊log₂ n⌋ + 1) |
|---|---|---|
| 9 | 9 | 4 |
| 1,000 | 1,000 | 10 |
| 1,000,000 | 1,000,000 | 20 |
| 1,000,000,000 | 1,000,000,000 | 30 |
When n doubles, linear search does twice the work; binary search does just one more comparison.
Searching
Linear and Binary Search: Try It
The array is sorted, so both methods give correct results. Orange: item being checked; red: discarded items; green: found item. Try different targets (first, last, a missing number) and watch the comparison counts.
Sorting
Sorting Algorithms
- Arrange the items of a collection by some rule.
- Usually in ascending or descending order.
- A basic step for data analysis and searching.
- Sorted data speeds up search: binary search works only on sorted arrays.
Shuffles the items; no guarantee of order.
Selects the smallest and puts it first.
Compares neighbors and moves the larger toward the end.
Faster methods exist (merge sort, quicksort); Java's built-in Arrays.sort uses refined versions of them.
Sorting
Random Sorting (Shuffling)
- Putting a collection into a random order: the items are shuffled.
- Uses: shuffling a deck of cards, reordering exam questions, generating test data.
- Some algorithms (e.g. quicksort) shuffle their input first to avoid bad cases.
Pros
- Gives a different arrangement every time.
- Built into Java:
Collections.shuffle(list).
Cons
- Does not sort the data.
- "Shuffle until sorted" (bogosort) is hopelessly slow: only one of the 3,628,800 orderings of 10 items is sorted.
Sorting · Step by step
Random Sorting: Step by Step
We start with an unsorted array: [42, 17, 8, 23, 31].
Step 1: the items are put in a random order: [31, 17, 42, 8, 23]. Each shuffle gives a different result.
Step 2: shuffled again: [8, 23, 31, 42, 17].
Step 3: shuffled again: [8, 23, 17, 31, 42]. Closer, but still not sorted: shuffling guarantees nothing.
Sorting
Selection Sort
- A simple comparison-based sorting algorithm.
- It repeatedly finds the smallest (or largest) item and swaps it into place.
Steps:
- Find the smallest item.
- Move it to the front (swap with the first item).
- Find the smallest of the rest.
- Move it to the second position.
- Repeat to the end.
Pros
- Simple and easy to follow.
- Good for small collections.
- At most n − 1 swaps.
Cons
- Slow on large collections: always n(n − 1)/2 comparisons (even if already sorted).
Sorting · Step by step
Selection Sort: Step by Step
We start with an unsorted array: [64, 25, 12, 22, 11, 36, 48].
Step 1: the minimum of the unsorted part (indices 0–6) is 11; it will swap with the 64 at position 1.
After step 1: [11, 25, 12, 22, 64, 36, 48]. The green part is sorted and in place.
Step 2: the minimum of the unsorted part (indices 1–6) is 12; it will swap with the 25 at position 2.
After step 2: [11, 12, 25, 22, 64, 36, 48]. The green part is sorted and in place.
Step 3: the minimum of the unsorted part (indices 2–6) is 22; it will swap with the 25 at position 3.
After step 3: [11, 12, 22, 25, 64, 36, 48]. The green part is sorted and in place.
Step 4: the minimum of the unsorted part (indices 3–6) is 25. Already in place: no change.
After step 4: [11, 12, 22, 25, 64, 36, 48]. The green part is sorted and in place.
Step 5: the minimum of the unsorted part (indices 4–6) is 36; it will swap with the 64 at position 5.
After step 5: [11, 12, 22, 25, 36, 64, 48]. The green part is sorted and in place.
Step 6: the minimum of the unsorted part (indices 5–6) is 48; it will swap with the 64 at position 6.
After step 6: [11, 12, 22, 25, 36, 48, 64]. The green part is sorted and in place. The last item falls into place on its own: 6 steps suffice for 7 items.
Sorting
Bubble Sort
- A simple algorithm that sorts by comparing items in sequence.
- It compares neighbors and carries the larger one toward the end, like a bubble rising to the surface.
Steps:
- Start at the beginning.
- Compare two neighbors; swap if needed so the larger moves right.
- Repeat to the end of the collection (one pass).
- The largest item is now last, so treat it as sorted.
- Repeat for the rest; stop when a pass makes no swaps.
Pros
- Simple and easy to follow.
- Good for small or nearly sorted collections (with early exit).
Cons
- Very slow on large collections: in the worst case n(n − 1)/2 comparisons and as many swaps.
Sorting · Step by step
Bubble Sort: Step by Step
We start with an unsorted array: [64, 25, 12, 22, 11, 36, 48].
Step 1 (pass 1): compare 64 and 25; 64 > 25, so swap → [25, 64, 12, 22, 11, 36, 48].
Step 2 (pass 1): compare 64 and 12; 64 > 12, so swap → [25, 12, 64, 22, 11, 36, 48].
Step 3 (pass 1): compare 64 and 22; 64 > 22, so swap → [25, 12, 22, 64, 11, 36, 48].
Step 4 (pass 1): compare 64 and 11; 64 > 11, so swap → [25, 12, 22, 11, 64, 36, 48].
Step 5 (pass 1): compare 64 and 36; 64 > 36, so swap → [25, 12, 22, 11, 36, 64, 48].
Step 6 (pass 1): compare 64 and 48; 64 > 48, so swap → [25, 12, 22, 11, 36, 48, 64].
Step 7 (pass 2): compare 25 and 12; 25 > 12, so swap → [12, 25, 22, 11, 36, 48, 64].
Step 8 (pass 2): compare 25 and 22; 25 > 22, so swap → [12, 22, 25, 11, 36, 48, 64].
Step 9 (pass 2): compare 25 and 11; 25 > 11, so swap → [12, 22, 11, 25, 36, 48, 64].
Step 10 (pass 2): compare 25 and 36; 25 < 36, no change.
Step 11 (pass 2): compare 36 and 48; 36 < 48, no change.
Step 12 (pass 3): compare 12 and 22; 12 < 22, no change.
Step 13 (pass 3): compare 22 and 11; 22 > 11, so swap → [12, 11, 22, 25, 36, 48, 64].
Step 14 (pass 3): compare 22 and 25; 22 < 25, no change.
Step 15 (pass 3): compare 25 and 36; 25 < 36, no change.
Step 16 (pass 4): compare 12 and 11; 12 > 11, so swap → [11, 12, 22, 25, 36, 48, 64].
Step 17 (pass 4): compare 12 and 22; 12 < 22, no change.
Step 18 (pass 4): compare 22 and 25; 22 < 25, no change.
Step 19 (pass 5): compare 11 and 12; 11 < 12, no change.
Step 20 (pass 5): compare 12 and 22; 12 < 22, no change.
Pass 5 made no swaps: the array is sorted and the algorithm stops. In total 20 comparisons, 11 swaps.
Sorting
Comparing the Sorting Methods
| Random (shuffle) | Selection | Bubble | |
|---|---|---|---|
| Idea | Produce a random order | Select the smallest, put it first | Compare neighbors, move the larger to the end |
| Sorted result? | No guarantee | Yes | Yes |
| Comparisons (n items) | — | always n(n − 1)/2 | worst n(n − 1)/2, n − 1 if sorted |
| Swaps | n − 1 | at most n − 1 | worst n(n − 1)/2 |
| Sample array (n = 7) | — | 21 comparisons, 5 actual swaps | 20 comparisons, 11 swaps |
Sorting
Five Sorting Algorithms Compared
| Algorithm | Idea | Average | Worst | Note |
|---|---|---|---|---|
| Bubble sort | compare two neighbors, swap if needed | O(n²) | O(n²) | simple; poor on large data |
| Selection sort | find the smallest, put it first; repeat on the rest | O(n²) | O(n²) | slow on large data |
| Insertion sort | insert the next item into its place in the sorted part | O(n²) | O(n²) | good for small or nearly sorted data |
| Merge sort | split in two, sort each half recursively, merge | O(n log n) | O(n log n) | stable; good on large data |
| Quicksort | pick a pivot, smaller left, larger right (divide and conquer) | O(n log n) | O(n²) | very fast on average |
Sorting
Selection and Bubble Sort: Try It
Each "one step" makes a single comparison. Orange: items being compared; green: items in their final place. Compare the two methods' comparison and swap counts on the same array.
Summary
Summary
| Algorithm | Key idea | Structure | Example |
|---|---|---|---|
| Ordering two / three numbers | Compare, print the smaller first | If … Else, nested decisions | 13, 11 → 11 13 |
| Factorial | Multiply 1 through n | Loop + accumulator (starts at 1) | 5! = 120 |
| Fibonacci | Each term is the sum of the previous two | Loop + shifting variables | 0 1 1 2 3 |
| Palindrome | Clean, reverse, compare | String operations, decision | kayak → yes |
| Prime | Look for a divisor in 2 … n/2 | Flag + Break | 21 → no |
| Power | Multiply the base exp times | Loop + accumulator | 3⁴ = 81 |
| Random / linear / binary search | Try at random / scan in order / halve | Loop, decision; binary needs a sorted array | at most 4 comparisons for 9 items (binary) |
| Shuffle / selection / bubble | Random order / select the smallest / swap neighbors | Nested loops, swaps | n(n − 1)/2 comparisons |
Same path every time: define the problem and tests → pseudocode → dry run → Java → run the tests.
Summary
Common Mistakes: Algorithms
factorial = 0 makes every product 0. Start sums at 0 and products at 1.-1: no error message, wrong answer.a = b; b = a; makes both equal to b. Save temp = a first.Summary
Common Mistakes: Java
| Mistake | What happens? |
|---|---|
Forgot import java.util.Scanner; | Compile error: error: cannot find symbol … symbol: class Scanner |
Missing ; at line end: int sum = 0 | Compile error: error: ';' expected |
Single = in a comparison: if (num % divisor = 0) | Compile error: error: unexpected type … required: variable, found: value. Test equality with ==. |
Comparing strings with == | Compiles, but gives false for "abcba" and its reverse. Use input.equals(rev). |
Looping with i <= arr.length | Runtime error: ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 |
Using int for 13! | No error, wrong result: 1932053504 (should be 6227020800). Use long. |
Summary
Sample Problems
- Trace the factorial algorithm for num = 4: what are
iandfactorialon each round? - What does the Fibonacci algorithm print for n = 7? How many times does the loop run?
- How many divisors does the prime algorithm try for 25, and what does it print?
- How many comparisons does linear search make for 31 and 50 in [42, 17, 8, 23, 31]?
- Which items does binary search inspect for 12 and 77 in [7, 12, 23, 31, 42, 48, 55, 64, 77]?
- In the power algorithm with base = 2, exp = 10, how many times does the loop run and what is the result?
Show answers
- (i, factorial): (1, 1), (2, 2), (3, 6), (4, 24). Result 24.
0 1 1 2 3 5 8. The first two terms print before the loop; it runs 5 times, for i = 2 … 6.- It would try divisors up to 25 / 2 = 12, but after 2, 3, 4 it finds 25 % 5 = 0 and breaks: 4 tries, "25 is not a prime number."
- 31: 5 comparisons (last item, found). 50: 5 comparisons, not found (−1).
- 12: 42 → 12 (2 comparisons, index 1). 77: 42 → 55 → 64 → 77 (4 comparisons, index 8).
- 10 times; the result is 1024.
Summary
Sample Problems: Sorting
- Sort [29, 10, 14, 37, 13] with selection sort, writing the array after each step.
- Apply bubble sort to [5, 1, 4, 2, 8]. What is the array after the first pass? How many passes with early exit?
- How many comparisons does selection sort make on 7 items? Would it change if the array were already sorted?
- How many comparisons does bubble sort (with early exit) make on a sorted array?
Show answers
- [10, 29, 14, 37, 13] → [10, 13, 14, 37, 29] → [10, 13, 14, 37, 29] (14 already in place) → [10, 13, 14, 29, 37].
- Pass 1: [1, 4, 2, 5, 8]; pass 2: [1, 2, 4, 5, 8]; pass 3 makes no swaps and the algorithm stops: 3 passes.
- 7 · 6 / 2 = 21 comparisons. No change: selection sort must look at every remaining item to find the minimum.
- n − 1 comparisons (one pass, no swaps); 6 for 7 items.
Summary
Exercises
- Write pseudocode and a Java program that find the largest of three integers; test all 6 orders.
- Sum 1 to n with a loop. For n = 100, compare with the formula n(n + 1)/2.
- Find the digit sum of a number (hint:
% 10and/ 10). What is it for 1234? - Change linear search to count how many times the target occurs.
- Change bubble sort to sort in descending order.
- Write Fibonacci in Java with
intand run it for n = 50. After which term do the results go wrong? Why?
Answers / hints
max = first; comparesecondandthirdwithmaxin turn and assign when larger. No need for 6 nested branches.- Start with
sum = 0, thensum = sum + i; for n = 100, 5050 = 100 · 101 / 2. while (num > 0) { sum += num % 10; num = num / 10; }→ 1 + 2 + 3 + 4 = 10.- Instead of
return, increment acountand let the loop run to the end; returncountat the end. - Only the comparison flips: swap if
arr[j] < arr[j + 1]. - F(46) = 1,836,311,903 is the last correct term; F(47) exceeds the
intlimit (2,147,483,647) and comes out negative (−1,323,752,223). Uselong.
Summary
References
- P. Deitel and H. Deitel, Java: How to Program (Early Objects), 11th ed., Pearson, 2017 (Chapter 4: algorithms and pseudocode; Chapter 19: searching and sorting).
- A. B. Downey and C. Mayfield, Think Java, 2nd ed., O'Reilly, 2019 (free online).
- R. Sedgewick and K. Wayne, Algorithms, 4th ed., Addison-Wesley, 2011 (Section 1.1 binary search, 2.1 elementary sorts).
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022.
- D. E. Knuth, The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed., Addison-Wesley, 1998.
- Oracle, The Java Tutorials: Language Basics, docs.oracle.com/javase/tutorial.