2 Complexity
2 Complexity
What’s an array?
An array is a fixed-size collection of elements of the same type. The elements of the
array are stored sequentially and can be accessed using their index.
1 17
Arrays
Types of arrays
1. Single Dimensional Arrays: Single dimensional array or 1-D array is the simplest form
of arrays. It consists of elements of similar types and these elements can be accessed
through their indices.
▶ Example: double my_array1[20];
2. Multi-dimensional Arrays: These arrays consist of elements that are array themselves.
2-D arrays are the most common type of multi-dimensional array that is used in the C
language. However, the number of dimensions can be more than 2.
▶ Example 1 (2-d array): int my_array1[5][10];
▶ Example 2 (3-d array): float my_array2[10][20][5];
2 17
Arrays
3 17
Binary search
4 17
Binary search
5 17
Binary search VI: exercice
Suppose we have a dictionary containing 240,000 words. In the worst case, how many
steps do you think each search will take?
Suppose you have a sorted array of 128 names, and you’re searching through it using
binary search. What’s the maximum number of steps it would take?
Suppose you double the size of the array. What’s the maximum number of steps now?
6 17
Algorithmic complexity
For any given problem, there are often numerous ways to write an algorithm that solves
it. Choosing the best algorithm involves analyzing its performance.
Different algorithms have different costs in terms of:
▶ execution time: number of operations performed by the algorithm.
▶ memory size: amount of memory required for the algorithm to complete.
These two concepts are called the time and space complexity of the algorithm.
Generally, the most important critiria to evaluate an algorithm is its time complexity.
7 17
Time complexity
instructions
Number of
tion
nc
y fu
xit
ple
m
Co
Input size
By analyzing the time complexity of an algorithm, we want to be able to answer questions like:
If we double the data, do we double the execution time, do we quadruple it, or something else
entirely?
8 17
Time complexity
Example: Calculate the number of instructions of the following algorithm which tries to find an
element x in table tab of n element:
for (int i = 0; i < n; i++) {
if (x == tab[i])
return true;
}
▶ number of instructions = comparison ∗(n + 1) + assignment ∗(n + 1) + comparison ∗n +
return
▶ The execution time of the different operations (comparison, assignment, etc) is assumed to
be the same. Let call this constant time c. So, the execution time T(n) of this algorithm is:
T (n) = c x (n + 1) + c x (n + 1) + c x n + c
▶ Since the duration c is not really defined, we can simply ignore the constants.
So, T (n) = 3n + 3
9 17
Types of complexities
10 17
Big O-notation
The Big-O notation (called also Landau notation) is a mathematical notation, used to
compare the rate of convergence of functions.
Let n → f (n) and n → g(n) be 2 functions defined over the natural numbers.
▶ We say that f = O(g) if and only if ∃n0 ∈ N+ , ∃c ∈ R∗+ , ∀n ≥ n0 , f (n) ≤ c ∗ g(n).
▶ In other words, lim f (n)/g(n) = c, which means that, f (n)/g(n) is bounded when n
n→∞
approaches infinity.
▶ g(n) is an asymptotic upper bound for f(n).
11 17
Big O-notation
Example 1:
▶ n2 + 1 is O(n2 ).
In fact, for n0 = 1 and c = 2, we have forall n ≥ n0 , n2 + 1 ≤ 2n2
▶ The definition is satisfied, so n2 + 1 = O(n2 ).
Example 2:
▶ f (n) = n4 + 100n2 + 5 = O(n4 ).
▶ for n0 = 11 and c = 2, we have forall n ≥ n0 , n4 + 100n2 + 5 ≤ 2n4
The Big-O notation makes it possible to represent the algorithmic complexity by taking
into account only the dominant term.
There is no unique set of values for n0 and c in proving the asymptotic bounds.
Example: 100n + 5 = O(n)
▶ Solution 1: n0 = 5 and c = 101.
▶ Soultion 2: n0 = 1 and c = 105
12 17
Common complexity classes
Here are some common complexity classes from the smallest to the biggest:
O(1): constant.
O(log(n)): logarithmic.
O(n): linear.
O(nlog(n)): linearithmic.
O(n2 ): quadratic.
O(n3 ): cubic.
2n : exponential.
n!: factorial.
13 17
Common complexity classes
Table bellow shows how long algorithms that use f(n) operations take to run on a
computer, where each operation costs one nanosecond (10−9 seconds).
14 17
Operations
15 17
Complexity of iterative algorithms
16 17
Exercice 1
Calculate the complexity of Selection sort algorithm:
void selection_sort(int arr[],int n)
{
int i, j;
int min;/* index of minimum */
int elt_min;
for (i =0;i<n; i++) {
min = i;
for (j = i +1;j<n; j++){
if (arr[j]< arr[min])
{
min = j;
}
}
elt_min = arr[min];
arr[min] = arr[i];
arr[i] = elt_min;
}
}
17 / 17