Professional Documents
Culture Documents
Characteristic operations
Exact values
Higher-level languages
Constant time
Linear time
Which is better?
and you want to test whether every element of the first set
(sub) also occurs in the second set (super):
System.out.println(subset(sub, super));
11
member
static boolean member(int x, int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
if (x == a[i]) return true;
}
return false;
}
12
subset
We would need to figure this out in order to compute average time complexity
14
15
We simplify n2 + 3n + 5 to just n2
16
Big O notation
17
18
Consider R = x2 + 3x + 5 as x varies:
x=
x=
x=
x=
x=
x=
0
10
100
1000
10,000
100,000
x2
x2
x2
x2
x2
x2
=
=
=
=
=
=
0
100
10000
1000000
108
1010
3x
3x
3x
3x
3x
3x
=
=
=
=
=
=
0
30
300
3000
3*104
3*105
5
5
5
5
5
5
=
=
=
=
=
=
5
5
5
5
5
5
R=
R=
R=
R=
R=
R=
5
135
10,305
1,003,005
100,030,005
10,000,300,005
19
y = x2 + 3x + 5, for x=1..10
20
y = x2 + 3x + 5, for x=1..20
21
WORSE
O(1)
O(log n)
O(n)
O(n log n)
O(n2)
O(n3)
O(nk)
O(2n)
constant time
log time
linear time
log linear time
quadratic time
cubic time
polynomial time
exponential time
22
The End
(for now)
23