Professional Documents
Culture Documents
Basic Concepts
Basic Concepts
Basic Concepts
Data Structures
Ching-Fang Hsu
Dept. of Computer Science and Information Engineering
National Cheng-Kung University
Algorithm Specification
3
3
Algorithm Specification
(contd.)
Example: Selection Sort
From those integers that are currently unsorted, find the
smallest and place it next in the sorted list.
Description statements; not an algorithm
Selection sort algorithm
for (i=0; i<n; i++) {
Examine list[i] to list[n-1] and
suppose that the smallest integer is
at list [min];
Interchange list[i] and list[min];
}
4
4
list[] [0 [1 [2 [3 [4 [5 [6 [7 [8 [9
] ] ] ] ] ] ] ] ] ]
201 34 76 9 102 15 63 85 396 27
i=0
list[i] list[min
]
list[] [0 [1 [2 [3 [4 [5 [6 [7 [8 [9
] ] ] ] ] ] ] ] ] ]
201
9 34 76 201 102 15 63 85 396 27
i=1
list[i list[min
] ] 5
Algorithm Specification
(contd.)
Example: Binary search
Given a sorted array list with n 1 distinct integers, figure
out if an integer searchnum is in list.
Binary search algorithm
}
6
int binarysearch (int list[], int searchnum, int left,
int right)
{ int middle;
while (left <= right) {
middle = (left + right)/2;
switch(COMPARE(list[middle], searchnum))
{ case -1: left = middle+1;
break;
case 0 : return middle;
case 1 : right = middle - 1;
}
}
return -1;
}
7
list[left] list[middle list[right
list[right]
] ]
… …
< list[middle] > list[middle]
8
Recursive Algorithms
9
int binarysearch (int list[], int searchnum, int
left,
int right))
{ int middle ;
if (left <= right) {
middle = (left + right)/2;
switch(COMPARE(list[middle], searchnum)) {
case -1 : return
binarysearch (list, searchnum, middle+1,
right);
case 0 : return middle;
case 1 : return
binarysearch (list, searchnum, left, middle -
1);
}
}
return -1;
}
10
Data Abstraction
11
Performance Analysis
12
Performance Analysis --
Asymptotic Notation ()
Definition: f(n) = (g(n)) iff there exist positive
constants c and n0 such that f(n) cg(n) for all n,
n n0 .
p.35, Example 1.15
(1) constant computing time, (n) linear, (n2)
quadratic, (2n) exponential
13
f(n) or
cg(n)
f(n) = 3n + 2
0 n
14
f(n) or
cg(n) cg(n), where g(n) = n
and c = 4
f(n) = 3n + 3
0 n
n0
15
Performance Analysis --
Asymptotic Notation () (contd.)
f(n) = (g(n)) only states that g(n) is an upper
bound on the value of f(n) for all n, n n0
instead of implying how good this bound is.
So, n = (n2), n = (n2.5), n = (n3), n = (2n), etc.
To be informative, g(n) should be as small a function
of n as one can come up with for which f(n) = (g(n)).
Theorem: If f(n) = amnm + + a1n + a0, then f(n)
= (nm).
16
Performance Analysis --
Asymptotic Notation () (contd.)
3n+2 = O(n), 3n+2 O(1)
3n+3 = O(n), 3n+3 = O(n2)
100n+6 = O(n)
10n2+4n+2 = O(n2), 10n2+4n+2 = O(n4), 10n2+4n+2 O(n)
1000n2+100n-6 = O(n2)
6*2n+n2 = O(2n)
17
Performance Analysis --
Asymptotic Notation () (contd.)
log n n nlog n n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65,536
5 32 160 1024 32,768 4,294,967,296
18
Performance Analysis --
Asymptotic Notation () (contd.)
n!
60 2n
n2
O(1) O(logn)
50
O(n) O(nlogn) O(n2
) O(n3
) O(2n
)
O(n!)
40
n log n
↑
30
f
20
n
10
log n
0
0 1 2 3 4 5 6 7 8 9 10
n → 19