Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Profile algorithms
Understand O notation's mathematical basis
Use O notation to measure running time
John Edgar
John Edgar
John Edgar
algorithm
More sophisticated timer classes exist
CPU speed
Amount of main memory
Specialized hardware (e.g. graphics card)
Operating system
System configuration (e.g. virtual memory)
Programming language
Algorithm implementation
Other programs
System tasks (e.g. memory management)
John Edgar
John Edgar
| ||| ||| ||| ||| ||| ||| ||| ||| ||| ||| |
declare and
initialize i
John Edgar
perform comparison,
print array element, and
increment i:10 times
make
comparison
when i = 10
10
some algorithm A
Denote a cost function as tA(n)
i.e. the running time of algorithm A, with input size n
John Edgar
11
declare and
initialize i
John Edgar
3n
perform comparison,
print array element, and
increment i: n times
1
make
comparison
when i = n
12
If the target is the first item in the array the search will be
very fast
John Edgar
13
John Edgar
14
John Edgar
15
John Edgar
16
18
John Edgar
19
John Edgar
20
Best case
The target is the first element of the array
Make 1 comparison
Worst case
The target is not in the array or
The target is at the last position in the array
Make n comparisons in either case
Average case
Is it (best case + worst case) / 2, i.e. (n + 1) / 2?
John Edgar
21
John Edgar
22
is at some location i
John Edgar
23
John Edgar
24
John Edgar
25
= (n / 2) + ((n + 1) / 4)
= (2n / 4) + ((n + 1) / 4)
= (3n + 1) / 4
John Edgar
26
John Edgar
27
28
John Edgar
29
algorithm is run
The barometer
instructions
Inequality comparison
One other operation (setting either lower or upper)
John Edgar
30
index
arr
11
13
17
19
23
mid = (0 + 7) / 2 = 3
John Edgar
31
element
arr
11
13
17
19
23
mid =
John Edgar
(0 + 7) / 2 = 3
(4 + 7) / 2 = 5
(6 + 7) / 2 = 6
done
32
33
Is the average case more like the best case or the worst
case?
What is the chance that an array element is the target
1/n the first time through the loop
1/(n/2) the second time through the loop
and so on
34
n
10
100
1,000
10,000
100,000
1,000,000
10,000,000
John Edgar
(3n+1)/4
8
76
751
7,501
75,001
750,001
7,500,001
log2(n)
3
7
10
13
17
20
24
35
Insertion Sort
each algorithm
This will include an analysis of how many times the
algorithms' loops iterate
John Edgar
37
unsorted part
John Edgar
38
23 41 33 81 07 19 11 45
07 41 33 81 23 19 11 45
07 11 33 81 23 19 41 45
07 11 19 81 23 33 41 45
07 11 19 23 81 33 41 45
07 11 19 23 33 81 41 45
07 11 19 23 33 41 81 45
07 11 19 23 33 41 45 81
John Edgar
39
John Edgar
Unsorted elements
n
n-1
Comparisons
n-1
n-2
3
2
1
2
1
0
n(n-1)/2
40
41
The barometer
instructions
Change smallest
Increment j
John Edgar
42
43
John Edgar
44
45
23 41 33 81 07 19 11 45
23 41 33 81 07 19 11 45
23 33 41 81 07 19 11 45
23 33 41 81 07 19 11 45
07 23 33 41 81 19 11 45
07 19 23 33 41 81 11 45
07 11 19 23 33 41 81 45
07 11 19 23 33 41 45 81
John Edgar
46
John Edgar
47
John Edgar
Sorted
Elements
0
Worst-case
Search
Worst-case
Shuffle
n-1
n-1
n-1
n(n-1)/2
n(n-1)/2
48
Requires n comparisons
John Edgar
49
John Edgar
50
John Edgar
51
John Edgar
53
John Edgar
31 12 07 23 93 02 11 18
54
31 12 07 23 93 02 11 18
John Edgar
55
31 12 07 23 93 02 11 18
John Edgar
56
John Edgar
31 12 07 23 93 02 11 18
57
31 12 07 23 93 02 31
11
11 18
John Edgar
58
John Edgar
11 12 07 23 93 02 31 18
59
John Edgar
11 12 07 02
23 93 23
02 31 18
60
11 12 07 02 93 23 31 18
John Edgar
61
11 12 07 02 18
93 23 31 93
18
John Edgar
62
11 12 07 02 18 23 31 93
smalls
pivot
bigs
John Edgar
63
00 08 07 01 06 02 05 09
Use the same algorithm to
partition this array into small
and big values
00 08 07 01 06 02 05 09
smalls
bigs!
pivot
John Edgar
64
09 08 07 06 05 04 02 01
Or this one:
01 08 07 06 05 04 02 09
smalls
pivot
John Edgar
bigs
65
John Edgar
66
67
47 70 36 97 03 67 29 11 48 09 53
36 09 29 48 03 11 47 53 97 61 70
36 09 03 11 29 47 48 53 61 70 97
08 01 11 29 36 47 48 53 61 70 97
09 03 11 29 36 47 48 53 61 70 97
03 09 11 29 36 47 48 53 61 70 97
John Edgar
68
each partition
This can be achieved by making them the
parameters of a recursive function
John Edgar
69
John Edgar
70
John Edgar
71
08 01 02 07 03 06 04 05
First partition
04 01 02 03 05 06 08 07
smalls
bigs
pivot
John Edgar
72
04 01 02 03 05 06 08 07
Second partition
pivot1
pivot2
02 01 03 04 05 06 07 08
sm1
John Edgar
big1
pivot1
sm2
big2
pivot2
73
02 01 03 04 05 06 07 08
Third partition
pivot1
done
done
done
01 02 03 04 05 06 07 08
pivot1
John Edgar
74
John Edgar
75
09 08 07 06 05 04 02 01
First partition
01 08 07 06 05 04 02 09
smalls
pivot
John Edgar
bigs
76
01 08 07 06 05 04 02 09
Second partition
01 08 07 06 05 04 02 09
smalls
John Edgar
bigs
pivot
77
01 08 07 06 05 04 02 09
Third partition
01 02 07 06 05 04 08 09
bigs
pivot
John Edgar
78
01 02 07 06 05 04 08 09
Fourth partition
01 02 07 06 05 04 08 09
smalls
pivot
John Edgar
79
01 02 07 06 05 04 08 09
Fifth partition
01 02 04 06 05 07 08 09
pivot
John Edgar
bigs
80
01 02 04 06 05 07 08 09
Sixth partition
01 02 04 06 05 07 08 09
smalls
John Edgar
pivot
81
01 02 04 06 05 07 08 09
Seventh partition!
01 02 04 05 06 07 08 09
pivot
John Edgar
82
log2(n) times
So in the worst case Quicksort performs around n2
operations
John Edgar
83
be partially sorted
John Edgar
84
John Edgar
86
John Edgar
87
John Edgar
88
Number of Operations
350
5(log2n)
3(n+1)/4
300
n
n(log2n)
250
n((n-1)/2)
n2
200
150
100
50
0
10
11
12
13
14
15
16
17
18
19
20
John Edgar
89
n2 and n(n-1)/2 are growing much faster than any of the others
12000
Number of Operations
10000
8000
log2n
5(log2n)
3(n+1)/4
6000
n
n(log2n)
4000
n((n-1)/2)
n2
2000
0
10
20
30
40
50
60
70
80
90
100
John Edgar
90
Number of Operations
1000000000000
800000000000
log2n
5(log2n)
3(n+1)/4
600000000000
n
n(log2n)
400000000000
n((n-1)/2)
n2
200000000000
0
10
50
100
500
1000
5000
10000
50000
100000
500000
1000000
John Edgar
91
Number of Operations
1000000000
100000000
log2n
10000000
5(log2n)
3(n+1)/4
1000000
100000
n(log2n)
10000
n((n-1)/2)
n2
1000
100
10
1
10
50
100
500
1000
5000
10000
50000
100000
500000
1000000
John Edgar
92
John Edgar
93
John Edgar
94
increases
John Edgar
95
John Edgar
96
John Edgar
97
John Edgar
98
John Edgar
99
John Edgar
100
John Edgar
101
John Edgar
102
1400
1200
1000
800
2n2+10n+6
3n2
600
400
200
0
5
John Edgar
10
11
12
13
14
15
16
17
18
19
20
103
John Edgar
104
John Edgar
105
John Edgar
106
time
The min is either at the first or the last element of the array
John Edgar
107
commonly used
Average case, often difficult to calculate
Best case, usually easy to calculate but less important than the
other cases
John Edgar
108
Linear search
Best case: O(1)
Average case: O(n)
Worst case: O(n)
Binary search
Best case: O(1)
Average case: O(log n)
Worst case: O(log n)
John Edgar
109
Quicksort
Best case: O(n(log2n))
Mergesort
Best case: O(n(log2n))
Average case: O(n(log2n))
Worst case: O(n(log2n))
John Edgar
110
Selection sort
Best Case: O(n2)
Average case: O(n2)
Worst case: O(n2)
Insertion sort
Best case: O(n)
111