Algorithms and Number Theory
Algorithms and Number Theory
Complexity
Complexity measures the efficiency of an algorithm, mainly in terms of time (how fast it runs)
and space (how much memory it uses). It helps us predict an algorithm's performance as the
input size grows.
● Time Complexity: Describes the amount of time an algorithm takes based on the size of
its input.
● Space Complexity: Describes the amount of memory an algorithm uses relative to the
input size.
Asymptotic Notation
To express complexity, we use asymptotic notations, which describe the behavior of
algorithms for large input sizes:
○ Example: O(n^2) means the time grows at most proportional to the square of
the input size.
● Theta Notation (Θ): Tight bound; describes both upper and lower bounds.
○ Example: Θ(n log n) means the time grows exactly proportional to n log n.
Common Complexities
Complexity Name Example
a. Modular Arithmetic
● Definition:
An arithmetic system for integers where numbers "wrap around" after a certain value, n.
● Notation:
a ≡ b (mod n) means (a-b) is divisible by n.
● Properties:
● Definition:
The largest number that divides two integers without a remainder.
Euclidean Algorithm:
GCD(a, b) = GCD(b, a mod b)
Example:
GCD(48, 18)
→ GCD(18, 48 mod 18) = GCD(18, 12)
→ GCD(12, 6)
→ GCD(6, 0)
→ GCD = 6
●
Combinatorial Analysis
1. Permutation and Combination
● Addition Rule:
If event A can happen in m ways and B in n ways, and they can't co-occur:
Total = m + n
● Multiplication Rule:
If event A can happen in m ways and event B in n ways:
Total = m × n
b. Permutations:
● Order matters.
c. Combinations:
● Order doesn't matter.
C(n, r) = n! / [r!(n - r)!]
●
a. Quick Sort
● Strategy: Pick a pivot, partition the array around the pivot, and sort the subarrays
recursively.
● Time Complexity:
b. Binary Search
● Strategy:
Repeatedly divide the sorted array in half to find the target.
● Time Complexity:
O(log n)
● Precondition:
The array must be sorted.
● After each full pass through the array, the largest unsorted element "bubbles" to its
correct position at the end.
Pseudocode
BubbleSort(arr):
n = length of arr
for i from 0 to n-1:
for j from 0 to n-i-2:
if arr[j] > arr[j+1]:
swap arr[j] with arr[j+1]
Example
Given array:
arr = [5, 2, 9, 1, 5, 6]
Pass 1:
[2, 5, 1, 5, 6, 9]
Pass 2:
[2, 1, 5, 5, 6, 9]
Pass 3:
[1, 2, 5, 5, 6, 9]
Time Complexity
Case Time
Average O(n²)
Case