Title: ywdeng@mail.knu.edu.tw
1????????????
- ???
- ywdeng_at_mail.knu.edu.tw
- http//w3.im.knu.edu.tw/joseph/
2??(Sorting)
- ????(Internal Sort)
- ???????????,????
- ????(External Sort)
- ???????,??????????,????????(?????)????
3??(Sorting)
- ????(Comparative Sort)
- ??????(Key Value)
- ????(Distributive Sort)
- ????????????
4??(Sorting)
- ???(Stability)
- ?????????R1, R2
- ?????R1??R2??,??????R1???R2??,????????????(Stable)
?????????????(Unstable)????
5????(Bubble Sort)
- ????
- ?N???????,??????????,??????????,????????
- N?????N-1???
- ?????O(N2)
- Stable
- Comparative Sort
6????(Bubble Sort)
7????(Bubble Sort)
void bubble_sort(int data, int size) int
base, compare, temp for (base 0 base lt
size - 1 base) / ???????,??????
/ for (compare base 1 compare lt
size compare) if (database gt
datacompare) temp
datacompare datacompare
database database temp
8????(Bubble Sort)JAVA
public BubbleSortDemo(int A) boolean done
false for (int i A.length (i gt 0)
(!done) i--) done true for
(int j 0 j lt i-1 j) if (Aj
gt Aj1) int t Aj
Aj Aj1 Aj1 t
done false
9?????(Insertion Sort)
- ?????K???????????L??
- ????,??????,????(????)
- ?????O(N2)
- Best Case????????????,???????????
- Stable
- Comparative Sort
10?????(Insertion Sort)
11?????(Insertion Sort)
void insertion_sort(int data, int size)
int base, compare, temp for (base 1 base
lt size base) /
????????,?????,??????????????? / temp
database compare base while
(compare gt 0 datacompare - 1 gt temp)
datacompare datacompare - 1
compare-- datacompare
temp
12????(Shell Sort)
- ????(Insertion Sort)????
- ????????,?????????????,????????????????
- ???????????(?????)??,????????????????????
- ???????????????
- ??????????????????,???????????????
- ?????
- ???N/2
- ???N/4
-
- ????1
13????(Shell Sort)
- ???????O(N2),??O(N1.5)
- Unstable
- Comparative Sort
14????(Shell Sort)
0
1
2
3
4
5
6
7
8
9
10
11
12
13/26
13/43
13/81
15????(Selection Sort)
- ??????????????????
- ???????????????????
-
- ?N-1?????N-1???????N-1???
- ?????O(N2),??????????????O(N)
- Stable
- Comparative Sort
16????(Selection Sort)
17????(Selection Sort)
void select_sort(int data, int size) int
base, compare, min, temp for (base 0 base
lt size - 1 base) /
???????????????? / min base
for (compare base 1 compare lt size
compare) if (datacompare lt
datamin) min compare
if (min ! base) temp datamin
datamin database
database temp
18????(Merge Sort)
- ????????????L1?L2????????????L
- ?????p?p1?p2????L?L1?L2???
- ??p1??????p2?????,??????p?????
- ??????????L1?L2?????
- ????
- ?????????????????,????????????
- ????????????
- ?????O(N log N),???????
- Stable
- Comparative Sort
- ??????
19????????????L1?L2????????????L
20????????
void merge(int data1, int data2, int data3,
int size1, int size2) int arg1 0, arg2
0, arg3 0 data1size1 32767
data2size2 32767 for ( arg3 lt size1
size2 arg3) / ?????,?????????????
/ if (data1arg1 lt data2arg2)
data3arg3 data1arg1
arg1 else data3arg3
data2arg2 arg2
21????(Quicksort)
- Divide-and-Conquer
- Partition-Exchange
- ???
- ?????????K1???,?N?????????,??????????K1?,?????????
??K1 - ???????????????Quicksort??
- ???????O(N log N),??O(N2)
- Unstable
- Comparative sort
22????(Quicksort)
23void quick_sort(int data, int left, int right,
int size) / left?right?????????? / int
lbase, rbase, temp if (left lt right)
lbase left 1 while (datalbase lt
dataleft) lbase rbase right
while (datarbase gt dataleft) rbase--
while (lbase lt rbase) /
?lbase??rbase,?????? / temp
datalbase datalbase
datarbase datarbase temp
lbase while (datalbase lt
dataleft) lbase rbase--
while (datarbase gt dataleft) rbase--
temp dataleft
dataleft datarbase datarbase
temp quick_sort(data, left, rbase - 1,
size) quick_sort(data, rbase 1, right,
size)
24????(Heap Sort)
- ???1
- ?N?????Heap????????,Heap?O(log N)??,??O(N log N)
- ?Heap??N???????????????O(log N),??N???O(N log N)
- ??????? O(N log N)
25????(Heap Sort)
- ????????,??N???
- ???2
- ????????Heap
- ??N??????Complete Binary Tree,?????????????
- Ai?????A2i,????A2i1,????Ai/2
- ?AN/2????Heap???????(AN/2?????Internal
Node),?????AN/2 -1,,??A1 - ?????O(N)
- ?N?????O(N log N)
26????????Heap
27????????Heap 1
28????????Heap 2
29????????Heap 3
30????????Heap 4
31????????Heap 5
32?????(Binary Tree Sort)
- ???????????(Binary Search Tree)
- ????(In-order Traversal)
33????(Radix Sort)
- ???Bucket Sort?Bin Sort
- ??????LSD(Least Significant Digit)?MSD(Most
Significant Digit)?? - d????d???
- ??Distribution Sort
- Stable
- ?????O(N)
- ??????????dN,d????
34????(Radix Sort)1radix10
35????(Radix Sort)2radix10
118
326,228
032
0
1
2
3
4
056
076,879
882
291,199
5
6
7
8
9
36????(Radix Sort)3radix10
032,056,076
118,199
228,291
326
0
1
2
3
4
879,882
5
6
7
8
9
37Radix Sort ???
Algorithm radix_sort(A, first, last,
maxDigits) // Sorts the array of positive decimal
integers Afirst, last // into ascending order
// maxDigits is the number of digits in the
longest integer for (i 1 to maxDigits)
Clear bucket0, bucket1, ..., bucket9
for (index first to last) digit
i-th digit from the right of Aindex
Place Aindex at end of bucketdigit
Place contents of bucket0, bucket1, ...,
bucket9 into the array A
38????????????
39??(Search)
- ?????????????????
- ????(Internal Search)
- ????(External Search)
40Sequential Search
- ???????
- Sequential Search ?????????
- ??? Linear Search ????
- ?????O(N)
- ??
- ?????????????????????
- ????????????,???????????????
41Sequential Search
int sequential_search(int data, int target, int
n) int i for (i 0 i lt n i)
if (target datai) return
i return -1
42Binary Search
- ??????????
- ?????????
- ???????????,??????????????,???????????????????????
????,??????????????,??????? - ?????O(log N)
43Binary Search
int binary_search(int data, int n, int target)
int left 0, right n-1, mid while
(left lt right) mid (left right) /
2 if (target datamid)
return mid else if (target gt
datamid) left mid 1
else right mid - 1
return -1
44Interpolation Search ???
- ???????(Uniform Distribution)
- ??s?????,x?????,low??????????????????(left),high
??????????????????(right)
45????
- Pattern Matching
- ??????????????????
- ?????????? ?? ????
- ??????????,????????
- ??? Brute Force Method
- ????????????????,????
- ?????O(NM),N?????????,M?????(Pattern)???
46????-???
int strfind(char string, char pattern)
int pos_s, pos_p, len_s, len_p len_s
strlen(string) len_p strlen(pattern)
/ ???string?????????????(len_s-len_p) / for
(pos_s 0 pos_s lt len_s - len_p pos_s)
for (pos_p 0 pos_p lt len_p pos_p)
/ ???????,???????? / if
(patternpos_p ! stringpos_s pos_p)
break / ?pos_p?pattern???????,??????
/ if (pos_p len_p - 1) return
pos_s return -1
/ ???? /
47????-KMP?
- Knuth-Morris-Pratt
- ?????????????Pattern??? ,???????????
- Failure function
- ?Pattern??????????(Prefix)??
Table of next possible matching positions
48????-KMP?
49Knuth-Morris-Pratt Algorithm
- Pre-compute table of next possible matching
positions - Try to match the characters
- If there is a mismatch, use the look-up table to
find where to start matching next - If the relevant part of the table is -1,
increment the position on the main string - If any other number, begin comparison at this
location on the pattern but at the current
position on the main string - If there is a complete match, you can find the
start position by taking the pattern index of the
last matched character away from the current
position - After a complete match, check the last position
in the next array this will tell you if you can
attempt another match starting in this string
again - Return matches
50Pre-compute table of next possible matching
positions
void preKmp(char x, int m, int kmpNext)
int i, j i 0 j kmpNext0 -1
while (i lt m) while (j gt -1 xi !
xj) j kmpNextj i
j if (xi xj) kmpNexti
kmpNextj else kmpNexti
j
- x the search pattern
- m length of the search pattern
51KMP Pattern Matching
void KMP(char x, int m, char y, int n) int
i, j, kmpNextXSIZE / Preprocessing /
preKmp(x, m, kmpNext) / Searching / i
j 0 while (j lt n) while (i gt -1
xi ! yj) i kmpNexti
i j if (i gt m)
OUTPUT(j - i) i kmpNexti
52???(Hashing)
- ????
- O(1) ??
- ??
- ??????
- ????????????????
- ??
- ????
- ??(Collision)????????????
- ???????? O(N) ??
53???????
- Hash function h(x)
- Map keys into positions in a table
- Hash table
- Bucket(?)
- Each position of the hash table
- Home bucket
- The position h(x)
- Slot(?)
- The number of elements that a bucket may hold
equals the number of slots in the bucket
54???????(Hash Table)
55???????
- Collision(??)
- ?h(x1)h(x2),x1?x2????????bucket?
- Overflow(??)
- ????bucket?collision????slot????
- Loading Factor ????
- Loading Density ????
- ?N/(SB) i.e.????/(????)
56???????
- ?????yf(x), x,y????
- ???xgty
- ??????????
- Avoid collision
- ????????key?????,??collision????
- Spread keys evenly in the array
- Uniform distribution
- Easy to compute
- The running time of the hash function should be
O(1)
57???????
- ??(Division)
- ????(Mid-Square)
- ?????(Digit Analysis)
58The Division Hash Function
- ??????? h(x)xM
- Many-to-one mapping
- ??
- ????
- M ???????????
- ??
- ???key????????bucket
59The Division Hash Function
public class DivisionMethod static int M
1031 //?? public static int h(int x)
return Math.abs(x) M
M ???? 20 ?????!
60The Division Hash Function
????8864, 100, 2, 77, 729
h(x) x 11
Hash Table
77
100
2
729
8864
0
1
2
3
4
5
6
7
8
9
10
61Middle-Square Method
- ??????
- ???????????????
- ???????????????W???,W2w(w?word size)
- Hash function
62Middle-Square Method
public class MiddleSquareMethod static int k
10 // M1024 static int w 32
public static int h(int x) return (x x)
gtgtgt (w - k)
??????LSB? (w-k)?????
63Middle-Square Method
5
1
0
3
2
4
X
5
8
4
9
7
6
2
6
0
4
3
0
X2
????,????
0
0
0
0
5
8
h(X)
gtgtgt??4?
64Middle-Square Method
- ??
- ????key????(Scatter)
- ??
- Keys with a large number of leading/trailing
zeros will collide - Middle-square method only consider a subset of
the bits in the middle
65?????(Digit Analysis)
- ????????
- ????? Digit ??????,??????? Digit ??,????????????
Digit ???
66???????(Overflow Handling)
- Linear probing ????
- ???????(?????)
- Rehashing ????
- ????????h1(x),h2(x),h3(x),,hm(x),?h1(x)????,???h2
(x),?????????h3(x),
67???????(Overflow Handling)
- Quadratic probing ????
- ? h(x) ??,????? ( h(x) i2 ) b ? ( h(x) i2 )
b, 1? i ? (b-1)/2 - Chaining ????
- ? Linked-List ????????????????
- Slot ????????
68Linear Probing ????
- Linear open addressing ??????
- When home bucket is full, collision item is
placed in the next available bucket - Regarding the table as circular
- Clustering problem
- ??????
69Clustering Problem
????88, 108, 48, 58, 228, 888
h(x) x 10
Hash Table
48
58
228
888
88
108
0
1
2
3
4
5
6
7
8
9
70Linear Probing ????
- ??
- Start from its home bucket
- If it is not in its home bucket, Looking forward
(circular) until - Found!
- An empty bucket is reachedgtelement does not
exist! - Return to the home bucketgtelement does not
exist! - Worst case insert and search time ?(n)
71Rehashing ????
- Use 2 distinct hash functions
- h K?0,1,,M-1
- hK?1,2,,M-1
- hi(x)(h(x)ih(x)) mod M