Data Structures An Introduction: William Fiset
Data Structures An Introduction: William Fiset
an Introduction
William Fiset
What is a Data Structure?
Dynamic Array
List Linked List
Tree Map
Map Hash Map / Hash Table
Golf Cart
Vehicle Bicycle
Smart Car
Computational
Complexity
William Fiset
Complexity Analysis
As programmers, we often find ourselves
asking the same two questions over and
over again:
O(f(n)) = O(n3)
a := 1 i := 0
b := 2 While i < 11 Do
c := a + 5*b i = i + 1
Big-O Examples
i := 0 i := 0
While i < n Do While i < n Do
i = i + 1 i = i + 3
f(n) = n f(n) = n/3
O(f(n)) = O(n) O(f(n)) = O(n)
Big-O Examples
Both of the following run in quadratic time.
The first may be obvious since n work done
n times is n*n = O(n2), but what about the
second one?
For (i := 0 ; i < n; i = i + 1)
For (j := 0 ; j < n; j = j + 1)
For (i := 0 ; i < n; i = i + 1)
For (j := i ; j < n; j = j + 1)
^ replaced 0 with i
Big-O Examples
For a moment just focus on the second loop.
Since i goes from [0,n) the amount of looping
done is directly determined by what i is.
Remark that if i=0, we do n work, if i=1, we do
n-1 work, if i=2, we do n-2 work, etc…
For (i := 0 ; i < n; i = i + 1)
For (j := i ; j < n; j = j + 1)
Big-O Examples
Suppose we have a sorted array and we want to
find the index of a particular value in the
array, if it exists. What is the time complexity
of the following algorithm?
low := 0
Ans: O(log2(n)) = O(log(n))
high := n-1
While low <= high Do