Algorithms

OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600

Searching

OIM3600

[1, 5, 10, 20, 50, 100, 500]

OIM3600

✉ ✉ ✉ ✉ ✉ ✉ ✉

[0] [1] [2] [3] [4] [5] [6]

OIM3600
input
→
algorithm
→
output
OIM3600
✉✉✉✉✉✉✉
→
algorithm
→
bool
OIM3600
OIM3600
OIM3600

Linear Search

OIM3600
for each ✉ from left to right
    if 50 is in the ✉
        return true
return false
OIM3600
for each ✉ from left to right
    if 50 is in the ✉
        return true
    else
        return false
OIM3600
for each ✉ from left to right
    if 50 is in the ✉
        return true
return false
OIM3600
for i from 0 to n-1
    if 50 is in ✉s[i]
        return true
return false
OIM3600

Binary Search

OIM3600
# Assuming the numbers are sorted

if 50 is in middle ✉
    return true
OIM3600
# Assuming the numbers are sorted

if 50 is in middle ✉
    return true
else if 50 < middle ✉
    search left half
else if 50 > middle ✉
    search right half   
OIM3600
# Assuming the numbers are sorted

if no ✉
    return false
if 50 is in middle ✉
    return true
else if 50 < middle ✉
    search left half
else if 50 > middle ✉
    search right half
OIM3600
# Assuming the numbers are sorted

if no ✉
    return false
if 50 is in ✉s[middle]
    return true
else if 50 < ✉s[middle]
    search ✉s[0] through ✉s[middle - 1]
else if 50 > ✉s[middle]
    search ✉s[middle + 1] through ✉s[n - 1]
OIM3600

Running Time

OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
O
OIM3600

O()

O()

O()

O()

O()

OIM3600

O()

O()

O()       linear search

O()

O()

OIM3600

O()

O()

O()       linear search

O()    binary search

O()

OIM3600

Ω

OIM3600

Ω()

Ω()

Ω()

Ω()

Ω()

OIM3600

Ω()

Ω()

Ω()

Ω()

Ω()       linear search, binary search

OIM3600

Θ

OIM3600

Sorting

OIM3600
input
→
algorithm
→
output
OIM3600
unsorted
→
algorithm
→
sorted
OIM3600
[7 2 5 4 1 6 0 3] →
algorithm
→ [0 1 2 3 4 5 6 7]
OIM3600

[7, 2, 5, 4, 1, 6, 0, 3]

OIM3600

Selection Sort

OIM3600
for i from 0 to n-1
    find smallest number between numbers[i] and numbers[n-1]
    swap smallest number with numbers[i]
OIM3600

O()

OIM3600

O()       selection sort

O()

O()

O()

O()

OIM3600

Ω()       selection sort

Ω()

Ω()

Ω()

Ω()

OIM3600

Θ()       selection sort

Θ()

Θ()

Θ()

Θ()

OIM3600

Bubble Sort

OIM3600

[7, 2, 5, 4, 1, 6, 0, 3]

OIM3600
repeat n-1 times
    for i from 0 to n-2
        if numbers[i] and numbers[i+1] out of order
            swap the two
OIM3600

O()

OIM3600

O()       bubble sort

O()

O()

O()

O()

OIM3600
repeat n-1 times
    for i from 0 to n-2
        if numbers[i] and numbers[i+1] out of order
            swap the two
    if no swaps
        quit    
OIM3600

Ω()

Ω()

Ω()       bubble sort

Ω()

Ω()

OIM3600

Algorithm Visualization

OIM3600

Recursion

OIM3600
# Assuming the numbers are already sorted

if no ✉
    return false
if 50 is in middle ✉
    return true
else if 50 < middle ✉
    search left half
else if 50 > middle ✉
    search right half   
OIM3600
# Assuming the numbers are already sorted

if no ✉
    return false
if 50 is in middle ✉
    return true
else if 50 < middle ✉
    search left half            # could use recursion
else if 50 > middle ✉
    search right half           # could use recursion
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600
OIM3600

Merge Sort

OIM3600
if only one number
    quit
else
    sort left half of numbers
    sort right half of numbers
    merge sorted halves
OIM3600

[7, 2, 5, 4, 1, 6, 0, 3]

OIM3600

7 | 2 | 5 | 4 | 1 | 6 | 0 | 3

OIM3600

7 | 2 | 5 | 4 | 1 | 6 | 0 | 3

2 7 | 4 5 | 1 6 | 0 3

OIM3600

7 | 2 | 5 | 4 | 1 | 6 | 0 | 3

2 7 | 4 5 | 1 6 | 0 3

2 4 5 7 | 0 1 3 6

OIM3600

7 | 2 | 5 | 4 | 1 | 6 | 0 | 3

2 7 | 4 5 | 1 6 | 0 3

2 4 5 7 | 0 1 3 6

0 1 2 3 4 5 6 7

OIM3600

O()

O()       merge sort

O()

O()

O()

OIM3600

Ω()

Ω()       merge sort

Ω()

Ω()

Ω()

OIM3600

Θ()

Θ()       merge sort

Θ()

Θ()

Θ()

OIM3600

Time

OIM3600

Space

OIM3600

The O notation (Big O) is used to represent the upper bound of an algorithm's running time, often in the worst-case scenario.

The Ω notation (Big Omega) is used to represent the lower bound of an algorithm's running time, often in the best-case scenario.

The Θ notation (Theta) is used to represent the precise or tight-bound complexity of an algorithm, capturing both upper and lower bounds, indicating that the algorithm's performance is consistently close to this bound.