Chapter 1.3: Algorithms

Object Oriented Programming with Java
Problem-solving stepsPseudocodeImplementation Sample algorithmsDry runsSearchingSorting
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Problem solving

Define a problem by its input, output and test cases; write the solution first as an algorithm, then as code.

Pseudocode and tracing

Express an algorithm in pseudocode and trace it by hand, step by step, with a variable table.

Translating to Java

Turn input, output, decision and loop steps into their Java equivalents.

Basic algorithms

Write and test algorithms for printing in order, factorial, Fibonacci, palindromes, primes and powers.

Searching

Compare random, linear and binary search; explain why binary search needs sorted data.

Sorting

Apply shuffling, selection sort and bubble sort to an array step by step.

Problem Solving

Algorithm First, Then Code

1Define theproblem2Design thealgorithm3Writethe code4Test andverifyfound a bug? go back and fix it
  • 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.
Analogy: a recipe is an algorithm. The ingredients are the input, the steps the algorithm, the dish the output. A vague step like "add a little salt" is not good enough for a computer.
Input

Zero or more initial values.

Output

At least one result.

Definiteness

Every step is clear and unambiguous.

Finiteness

It ends after finitely many steps.

Effectiveness

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.
input3, 5programoutput8=expected: 8test passed

Problem: add two numbers

TestInputExpected output
13, 58
2−2, 75
3 (edge)0, 00

Problem: count the words in a text

TestInputExpected 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:

  1. Get the first number.
  2. Get the second number.
  3. Add the two numbers.
  4. 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

Stepnum1num2sumScreen
1. Get the first number3
2. Get the second number35
3. Add the two numbers358
4. Print the sum3588

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.

PseudocodeMeaningJava
Start … EndStart and end of the algorithmthe { … } body of main
Read xGet a value from the userx = scanner.nextInt();
Print "…", xPrint to the screenSystem.out.println("…" + x);
x = 5Assign a value to a variablex = 5;
If x = y thenTest for equalityif (x == y) (two equals signs!)
ElseIf the condition is falseelse
For i = 1 to nRepeatfor (int i = 1; i <= n; i++)
BreakLeave the loop at oncebreak;
%, and, orRemainder, 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.
PseudocodealgorithmAddTwo.javasource codeAddTwo.classbytecodeOutputJVM runs itcodingjavacjava
Algorithm stepJava
Get the first numbernum1 = scanner.nextDouble();
Get the second numbernum2 = scanner.nextDouble();
Add the two numberssum = num1 + num2;
Print the sumSystem.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

double num1double num2double sum

A Scanner is created to read the keyboard; no numbers yet.

double num1double num2double sum

print writes the prompt without a newline; the cursor waits right after it.

double num13.0double num2double sum

The user types 3 and presses Enter; nextDouble() reads it as 3.0 into num1.

double num13.0double num2double sum

The second prompt is printed.

double num13.0double num25.0double sum

The user enters 5; num2 = 5.0.

double num13.0double num25.0double sum8.0

The right side is evaluated first: 3.0 + 5.0 = 8.0; the result goes into sum.

double num13.0double num25.0double sum8.0

+ joins strings; the numbers are converted to text to form one line.

double num13.0double num25.0double sum8.0

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.

Printing two integers in order

Decision: If … Else

Printing three integers in order

Nested decisions, and

Factorial

A loop and an accumulator

Fibonacci sequence

Shifting variables in a loop

Palindrome check

Clean, reverse, compare

Prime check

A flag and breaking out of a loop

Powers

Repeated multiplication

Searching and sorting

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.

InputExpected
4, 114 11
13, 1111 13
7, 77 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)

firstNumsecondNum

The user is asked for the first number.

firstNum4secondNum

The user enters 4; it goes into the firstNum box.

firstNum4secondNum11

The second number is requested; the user enters 11.

firstNum4secondNum11<true

Condition: 4 < 11 → true.

firstNum4secondNum11<true

The condition is true, so the then branch runs: first, then second.

firstNum4secondNum11

The algorithm ends. Output 4 11, as expected.

firstNumsecondNum

Second test: the algorithm runs again with 13 and 11.

firstNum13secondNum

The user enters 13; it goes into the firstNum box.

firstNum13secondNum11

The second number is requested; the user enters 11.

firstNum13secondNum11≮false

Condition: 13 < 11 → false.

firstNum13secondNum11≮false

False: we move to the Else branch.

firstNum13secondNum11≮false

The second (smaller) prints first, then the first.

firstNum13secondNum11

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)

first7second3third57 ≤ 3 ? false

Input 7, 3, 5. First condition: 7 ≤ 3 is false; the right side of && is not even evaluated.

first7second3third53 ≤ 7 and 3 ≤ 5 : true

Second condition: 3 ≤ 7 is true and 3 ≤ 5 is true → the smallest is second.

first7second3third5smallest: 3

The smallest is printed; print adds no newline.

first7second3third57 ≤ 5 ? false

The other two are compared: 7 ≤ 5 is false.

first7second3third5order: 3 5 7

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.

nn!
01
11
36
5120
103,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)

12345××××factorial = 1

The user enters 5.

12345××××factorial = 1

The accumulator starts at 1.

12345××××factorial = 1

Loop: i = 1 (i ≤ 5, so the body runs).

12345××××factorial = 1

factorial = 1 × 1 = 1.

12345××××factorial = 1

Loop: i = 2 (i ≤ 5, so the body runs).

12345××××factorial = 1 × 2 = 2

factorial = 1 × 2 = 2.

12345××××factorial = 1 × 2 = 2

Loop: i = 3 (i ≤ 5, so the body runs).

12345××××factorial = 1 × 2 × 3 = 6

factorial = 2 × 3 = 6.

12345××××factorial = 1 × 2 × 3 = 6

Loop: i = 4 (i ≤ 5, so the body runs).

12345××××factorial = 1 × 2 × 3 × 4 = 24

factorial = 6 × 4 = 24.

12345××××factorial = 1 × 2 × 3 × 4 = 24

Loop: i = 5 (i ≤ 5, so the body runs).

12345××××factorial = 1 × 2 × 3 × 4 × 5 = 120

factorial = 24 × 5 = 120.

12345××××factorial = 1 × 2 × 3 × 4 × 5 = 120

i becomes 6; 6 ≤ 5 is false, so the loop ends.

12345××××factorial = 1 × 2 × 3 × 4 × 5 = 120

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
10
20 1
50 1 1 2 3
70 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)

01234

The user enters n = 5: we want the first 5 terms.

0011234ab

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

0011234ab

5 ≥ 1: print a.

0011234ab

5 ≥ 2: print b.

0011234ab

Loop: i = 2 (i ≤ n − 1 = 4).

00111234ab

result = a + b = 0 + 1 = 1.

00111234a,b

a = b → a = 1 (shifted one step).

00111234ab

b = result → b = 1.

00111234ab

The new term is printed: 1.

00111234ab

Loop: i = 3 (i ≤ n − 1 = 4).

001112234ab

result = a + b = 1 + 1 = 2.

001112234a,b

a = b → a = 1 (shifted one step).

001112234ab

b = result → b = 2.

001112234ab

The new term is printed: 2.

001112234ab

Loop: i = 4 (i ≤ n − 1 = 4).

0011122334ab

result = a + b = 1 + 2 = 3.

0011122334a,b

a = b → a = 2 (shifted one step).

0011122334ab

b = result → b = 3.

0011122334ab

The new term is printed: 3.

0011122334ab

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.

InputExpected
abcbapalindrome
Kayakpalindrome
Kitapnot

Sample Algorithms · Dry run

Palindrome: Dry Run (abcba and Kitap)

inputTextabcba

The user enters abcba.

inputTextabcbacleanTextabcba

Cleaning: convert to lowercase (no change).

cleanTextabcbareversedabcba

The clean text read backwards: abcba.

cleanTextabcbareversedabcba

Compare letter by letter: all letters match → true.

cleanTextabcbareversedabcba

True: the palindrome message prints.

inputTextKitap

Second test: the user enters Kitap.

inputTextKitapcleanTextkitap

Cleaning: convert to lowercase ("Kitap" → "kitap").

cleanTextkitapreversedpatik

The clean text read backwards: patik.

cleanTextkitapreversedpatik

Compare letter by letter: kitap ≠ patik → false.

cleanTextkitapreversedpatik

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 / 2 is 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)

divisor:23no divisor tried yet

The user enters 7.

divisor:23no divisor tried yet

7 ≥ 2, so the flag starts as True.

divisor:237 % 2 = 1

Loop: divisor = 2 (≤ 7 / 2 = 3).

divisor:237 % 2 = 1 → not divisible

7 % 2 = 1 ≠ 0 → not divisible, go on.

divisor:237 % 3 = 1

Loop: divisor = 3 (≤ 7 / 2 = 3).

divisor:237 % 3 = 1 → not divisible

7 % 3 = 1 ≠ 0 → not divisible, go on.

divisor:237 % 3 = 1 → not divisible

divisor becomes 4; 4 ≤ 3 is false, the loop ends. No divisor found.

divisor:237 % 3 = 1 → not divisible

The flag is True: 7 is prime.

divisor:2345678910no divisor tried yet

Second test: the user enters 21.

divisor:2345678910no divisor tried yet

21 ≥ 2, so the flag starts as True.

divisor:234567891021 % 2 = 1

Loop: divisor = 2 (≤ 21 / 2 = 10).

divisor:234567891021 % 2 = 1 → not divisible

21 % 2 = 1 ≠ 0 → not divisible, go on.

divisor:234567891021 % 3 = 0

Loop: divisor = 3 (≤ 21 / 2 = 10).

divisor:234567891021 % 3 = 0 → divisible

21 % 3 = 0 → divisible!

divisor:234567891021 % 3 = 0 → divisible

The flag is lowered: isPrime = False.

divisor:234567891021 % 3 = 0 → divisible

Break: the remaining divisors are skipped.

divisor:234567891021 % 3 = 0 → divisible

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.

baseexpExpected
3481
2101024
501

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)

3333×××result = 1

The user enters base = 3.

3333×××result = 1

The user enters exp = 4: we will compute 34.

3333×××result = 1

The accumulator starts at 1.

3333×××result = 1

Loop: i = 1.

3333×××result = 3

result = 1 × 3 = 3.

3333×××result = 3

Loop: i = 2.

3333×××result = 9

result = 3 × 3 = 9.

3333×××result = 9

Loop: i = 3.

3333×××result = 27

result = 9 × 3 = 27.

3333×××result = 27

Loop: i = 4.

3333×××result = 81

result = 27 × 3 = 81.

3333×××result = 81

i = 5 > 4: the loop ends.

3333×××result = 81

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.
Problem definition

Input:

  • A collection of objects, say a "Basket".
  • A particular object, say an "Apple".

Output:

  • true if the "Apple" is in the basket.
  • false if it is not.

In code we usually return the index (position) of the item, or -1 if it is missing.

Searching

Three Search Methods

Random

Each step picks a random item and compares it with the target.

238421731548123random order, may repeat

No order needed · no guarantee of finding it

Linear

Compares the items one by one from start to end.

238421731548left to right, one by one

No order needed · at most n comparisons

Binary

Looks at the middle item and discards half the range each step.

5121823314248middiscardedsearch right half

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:
  1. Pick a random item.
  2. Compare it with the target.
  3. If it is the target, stop.
  4. 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

77042164212323455576487318target: 48

We have an unsorted array; the target is 48.

77042164212323455576487318pickedcomparing 23 with 48

Step 1: the random pick is 23; 23 ≠ 48, not found yet. Pick another.

77042164212323455576487318pickedcomparing 42 with 48

Step 2: the random pick is 42; 42 ≠ 48, not found yet. Pick another.

77042164212323455576487318pickedcomparing 7 with 48

Step 3: the random pick is 7; 7 ≠ 48, not found yet. Pick another.

77042164212323455576487318pickedcomparing 48 with 48

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:

  1. Put the items in a list (array).
  2. Starting from the first, compare each item with the target in turn.
  3. If it is the target, stop and return its position.
  4. Otherwise move to the next item.
  5. 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

42017182233314iarr[0] = 42 vs 23

Step 1: the target is 23. Start with the first item (42) and compare it with the target.

42017182233314iarr[1] = 17 vs 23

Step 2: the first item (42) does not match 23. Move on: 17.

42017182233314iarr[2] = 8 vs 23

Step 3: the second item (17) does not match either. Next: 8.

42017182233314iarr[3] = 23 vs 23

Step 4: the third item (8) does not match either. Next: 23.

42017182233314iarr[3] = 23: found

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:

  1. Take the middle item and compare it with the target.
  2. If it is the target, stop.
  3. If the target is smaller, focus on the left half.
  4. If the target is larger, focus on the right half.
  5. 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

70121232313424485556647778lowhightarget: 48

We have a sorted array; the target is 48. At first the range is the whole array: low = 0, high = 8.

70121232313424485556647778lowmidhighmid = (0 + 8) / 2 = 4 → 42

Step 1: take the middle item (42) and compare it with the target (48).

70121232313424485556647778lowhigh48 > 42 → right half

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].

70121232313424485556647778lowmidhighmid = (5 + 8) / 2 = 6 → 55

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.

70121232313424485556647778low,high48 < 55 → left half

Step 4: 48 is less than the middle item (55); the right side is dropped. Only [48] remains.

70121232313424485556647778low,mid,highmid = 5 → 48 = 48

Step 5: the middle item, 48, equals the target: found. Just 3 comparisons in a 9-item array.

Searching

Binary vs. Linear Search

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.
Binary search
  • 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 nLinear (n)Binary (⌊log₂ n⌋ + 1)
994
1,0001,00010
1,000,0001,000,00020
1,000,000,0001,000,000,00030

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.
before421782331after817233142sort (ascending)
Random

Shuffles the items; no guarantee of order.

Selection sort

Selects the smallest and puts it first.

Bubble sort

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

42017182233314initial array

We start with an unsorted array: [42, 17, 8, 23, 31].

31017142283234shuffle 1

Step 1: the items are put in a random order: [31, 17, 42, 8, 23]. Each shuffle gives a different result.

80231312423174shuffle 2

Step 2: shuffled again: [8, 23, 31, 42, 17].

80231172313424shuffle 3

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:

  1. Find the smallest item.
  2. Move it to the front (swap with the first item).
  3. Find the smallest of the rest.
  4. Move it to the second position.
  5. 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

640251122223114365486start

We start with an unsorted array: [64, 25, 12, 22, 11, 36, 48].

640251122223114365486iminmin of unsorted part: 11

Step 1: the minimum of the unsorted part (indices 0–6) is 11; it will swap with the 64 at position 1.

110251122223644365486after step 1

After step 1: [11, 25, 12, 22, 64, 36, 48]. The green part is sorted and in place.

110251122223644365486iminmin of unsorted part: 12

Step 2: the minimum of the unsorted part (indices 1–6) is 12; it will swap with the 25 at position 2.

110121252223644365486after step 2

After step 2: [11, 12, 25, 22, 64, 36, 48]. The green part is sorted and in place.

110121252223644365486iminmin of unsorted part: 22

Step 3: the minimum of the unsorted part (indices 2–6) is 22; it will swap with the 25 at position 3.

110121222253644365486after step 3

After step 3: [11, 12, 22, 25, 64, 36, 48]. The green part is sorted and in place.

110121222253644365486i, minmin of unsorted part: 25

Step 4: the minimum of the unsorted part (indices 3–6) is 25. Already in place: no change.

110121222253644365486after step 4

After step 4: [11, 12, 22, 25, 64, 36, 48]. The green part is sorted and in place.

110121222253644365486iminmin of unsorted part: 36

Step 5: the minimum of the unsorted part (indices 4–6) is 36; it will swap with the 64 at position 5.

110121222253364645486after step 5

After step 5: [11, 12, 22, 25, 36, 64, 48]. The green part is sorted and in place.

110121222253364645486iminmin of unsorted part: 48

Step 6: the minimum of the unsorted part (indices 5–6) is 48; it will swap with the 64 at position 6.

110121222253364485646after step 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:

  1. Start at the beginning.
  2. Compare two neighbors; swap if needed so the larger moves right.
  3. Repeat to the end of the collection (one pass).
  4. The largest item is now last, so treat it as sorted.
  5. 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

640251122223114365486start

We start with an unsorted array: [64, 25, 12, 22, 11, 36, 48].

250641122223114365486jj+1pass 1 · swapped

Step 1 (pass 1): compare 64 and 25; 64 > 25, so swap → [25, 64, 12, 22, 11, 36, 48].

250121642223114365486jj+1pass 1 · swapped

Step 2 (pass 1): compare 64 and 12; 64 > 12, so swap → [25, 12, 64, 22, 11, 36, 48].

250121222643114365486jj+1pass 1 · swapped

Step 3 (pass 1): compare 64 and 22; 64 > 22, so swap → [25, 12, 22, 64, 11, 36, 48].

250121222113644365486jj+1pass 1 · swapped

Step 4 (pass 1): compare 64 and 11; 64 > 11, so swap → [25, 12, 22, 11, 64, 36, 48].

250121222113364645486jj+1pass 1 · swapped

Step 5 (pass 1): compare 64 and 36; 64 > 36, so swap → [25, 12, 22, 11, 36, 64, 48].

250121222113364485646jj+1pass 1 · swapped

Step 6 (pass 1): compare 64 and 48; 64 > 48, so swap → [25, 12, 22, 11, 36, 48, 64].

120251222113364485646jj+1pass 2 · swapped

Step 7 (pass 2): compare 25 and 12; 25 > 12, so swap → [12, 25, 22, 11, 36, 48, 64].

120221252113364485646jj+1pass 2 · swapped

Step 8 (pass 2): compare 25 and 22; 25 > 22, so swap → [12, 22, 25, 11, 36, 48, 64].

120221112253364485646jj+1pass 2 · swapped

Step 9 (pass 2): compare 25 and 11; 25 > 11, so swap → [12, 22, 11, 25, 36, 48, 64].

120221112253364485646jj+1pass 2 · no change

Step 10 (pass 2): compare 25 and 36; 25 < 36, no change.

120221112253364485646jj+1pass 2 · no change

Step 11 (pass 2): compare 36 and 48; 36 < 48, no change.

120221112253364485646jj+1pass 3 · no change

Step 12 (pass 3): compare 12 and 22; 12 < 22, no change.

120111222253364485646jj+1pass 3 · swapped

Step 13 (pass 3): compare 22 and 11; 22 > 11, so swap → [12, 11, 22, 25, 36, 48, 64].

120111222253364485646jj+1pass 3 · no change

Step 14 (pass 3): compare 22 and 25; 22 < 25, no change.

120111222253364485646jj+1pass 3 · no change

Step 15 (pass 3): compare 25 and 36; 25 < 36, no change.

110121222253364485646jj+1pass 4 · swapped

Step 16 (pass 4): compare 12 and 11; 12 > 11, so swap → [11, 12, 22, 25, 36, 48, 64].

110121222253364485646jj+1pass 4 · no change

Step 17 (pass 4): compare 12 and 22; 12 < 22, no change.

110121222253364485646jj+1pass 4 · no change

Step 18 (pass 4): compare 22 and 25; 22 < 25, no change.

110121222253364485646jj+1pass 5 · no change

Step 19 (pass 5): compare 11 and 12; 11 < 12, no change.

110121222253364485646jj+1pass 5 · no change

Step 20 (pass 5): compare 12 and 22; 12 < 22, no change.

110121222253364485646array sorted

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)SelectionBubble
IdeaProduce a random orderSelect the smallest, put it firstCompare neighbors, move the larger to the end
Sorted result?No guaranteeYesYes
Comparisons (n items)—always n(n − 1)/2worst n(n − 1)/2, n − 1 if sorted
Swapsn − 1at most n − 1worst n(n − 1)/2
Sample array (n = 7)—21 comparisons, 5 actual swaps20 comparisons, 11 swaps
Selection and bubble sort grow with n²: about 500,000 comparisons for 1,000 items and about 500 billion for 1,000,000. Large data calls for smarter algorithms (e.g. merge sort, n · log₂ n).

Sorting

Five Sorting Algorithms Compared

AlgorithmIdeaAverageWorstNote
Bubble sortcompare two neighbors, swap if neededO(n²)O(n²)simple; poor on large data
Selection sortfind the smallest, put it first; repeat on the restO(n²)O(n²)slow on large data
Insertion sortinsert the next item into its place in the sorted partO(n²)O(n²)good for small or nearly sorted data
Merge sortsplit in two, sort each half recursively, mergeO(n log n)O(n log n)stable; good on large data
Quicksortpick a pivot, smaller left, larger right (divide and conquer)O(n log n)O(n²)very fast on average
An n · log n algorithm makes about 20 million comparisons for 1,000,000 items; an n² algorithm makes about 500 billion.

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

AlgorithmKey ideaStructureExample
Ordering two / three numbersCompare, print the smaller firstIf … Else, nested decisions13, 11 → 11 13
FactorialMultiply 1 through nLoop + accumulator (starts at 1)5! = 120
FibonacciEach term is the sum of the previous twoLoop + shifting variables0 1 1 2 3
PalindromeClean, reverse, compareString operations, decisionkayak → yes
PrimeLook for a divisor in 2 … n/2Flag + Break21 → no
PowerMultiply the base exp timesLoop + accumulator3⁴ = 81
Random / linear / binary searchTry at random / scan in order / halveLoop, decision; binary needs a sorted arrayat most 4 comparisons for 9 items (binary)
Shuffle / selection / bubbleRandom order / select the smallest / swap neighborsNested loops, swapsn(n − 1)/2 comparisons

Same path every time: define the problem and tests → pseudocode → dry run → Java → run the tests.

Summary

Common Mistakes: Algorithms

Wrong accumulator startStarting with factorial = 0 makes every product 0. Start sums at 0 and products at 1.
Off-by-one loop boundsWriting 2 … n instead of 2 … n − 1 in Fibonacci gives n + 1 terms. Check bounds with a small dry run.
Binary search on unsorted dataSearching [77, 42, 64, 12, 23, 55, 7, 48, 31] for 77 returns -1: no error message, wrong answer.
Forgetting edge casesTest 0 and 1 for primes, 0! for factorial and n = 1 for Fibonacci separately.
Random search with no stopping ruleIf the target is absent the loop runs forever; it needs a limit on tries.
Swapping without a temporarya = b; b = a; makes both equal to b. Save temp = a first.

Summary

Common Mistakes: Java

MistakeWhat happens?
Forgot import java.util.Scanner;Compile error: error: cannot find symbol … symbol: class Scanner
Missing ; at line end: int sum = 0Compile 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.lengthRuntime 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

  1. Trace the factorial algorithm for num = 4: what are i and factorial on each round?
  2. What does the Fibonacci algorithm print for n = 7? How many times does the loop run?
  3. How many divisors does the prime algorithm try for 25, and what does it print?
  4. How many comparisons does linear search make for 31 and 50 in [42, 17, 8, 23, 31]?
  5. Which items does binary search inspect for 12 and 77 in [7, 12, 23, 31, 42, 48, 55, 64, 77]?
  6. In the power algorithm with base = 2, exp = 10, how many times does the loop run and what is the result?
Show answers
  1. (i, factorial): (1, 1), (2, 2), (3, 6), (4, 24). Result 24.
  2. 0 1 1 2 3 5 8. The first two terms print before the loop; it runs 5 times, for i = 2 … 6.
  3. 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."
  4. 31: 5 comparisons (last item, found). 50: 5 comparisons, not found (−1).
  5. 12: 42 → 12 (2 comparisons, index 1). 77: 42 → 55 → 64 → 77 (4 comparisons, index 8).
  6. 10 times; the result is 1024.

Summary

Sample Problems: Sorting

  1. Sort [29, 10, 14, 37, 13] with selection sort, writing the array after each step.
  2. Apply bubble sort to [5, 1, 4, 2, 8]. What is the array after the first pass? How many passes with early exit?
  3. How many comparisons does selection sort make on 7 items? Would it change if the array were already sorted?
  4. How many comparisons does bubble sort (with early exit) make on a sorted array?
Show answers
  1. [10, 29, 14, 37, 13] → [10, 13, 14, 37, 29] → [10, 13, 14, 37, 29] (14 already in place) → [10, 13, 14, 29, 37].
  2. 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.
  3. 7 · 6 / 2 = 21 comparisons. No change: selection sort must look at every remaining item to find the minimum.
  4. n − 1 comparisons (one pass, no swaps); 6 for 7 items.

Summary

Exercises

  1. Write pseudocode and a Java program that find the largest of three integers; test all 6 orders.
  2. Sum 1 to n with a loop. For n = 100, compare with the formula n(n + 1)/2.
  3. Find the digit sum of a number (hint: % 10 and / 10). What is it for 1234?
  4. Change linear search to count how many times the target occurs.
  5. Change bubble sort to sort in descending order.
  6. Write Fibonacci in Java with int and run it for n = 50. After which term do the results go wrong? Why?
Answers / hints
  1. max = first; compare second and third with max in turn and assign when larger. No need for 6 nested branches.
  2. Start with sum = 0, then sum = sum + i; for n = 100, 5050 = 100 · 101 / 2.
  3. while (num > 0) { sum += num % 10; num = num / 10; } → 1 + 2 + 3 + 4 = 10.
  4. Instead of return, increment a count and let the loop run to the end; return count at the end.
  5. Only the comparison flips: swap if arr[j] < arr[j + 1].
  6. F(46) = 1,836,311,903 is the last correct term; F(47) exceeds the int limit (2,147,483,647) and comes out negative (−1,323,752,223). Use long.

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.

The End

Chapter 1.3: Algorithms
Previous: Chapter 1.2: Computation and Computational Thinking · Next: Chapter 1.4: Flowcharts
1 / 1 Sercan KÜLCÜ. All rights reserved.