Title: Recitation 3 - Data Structures 07b
1???? ?????? 08
2???? CountSort
???? n ?????? ????? ????? 1-k
????? ????? ?????? A ????? k
1
1
3 , 5 , 7 , 1 , 9 , 6
2
3
1
4
1
3
5
6
7
9
5
1
- ??? ?? A.
- ??? x ???? ???Ax 1
- ???? ?? ????? ?????for (i 1 i lt k i)
- if (Ai 1 ) output i
6
1
7
1
8
9
1
3???????? ???? CountSort
???? n ?????? ????? ????? 1-k
- ??? ?? A.
- ??? x ???? ???Ax 1
- ???? ?? ????? ?????for (i 1 i lt k i)
- if (Ai 1 ) output i
??????? ???
??
??????? ???
????? ???? ??????!
4???? CountSort
???? n ?????? ????? ????? ????? ????? 1-k
3 , 5 , 3 , 1 , 9 , 5 , 3
31 , 51 , 32 , 11 , 91 , 52 , 33
1
11
2
???? ???? Stable sort
3
31
32
33
4
11, 31 , 32 , 33 , 51 , 52, 91
5
51
52
6
7
????? ????? A ????? k ?? ?????
8
9
91
??????? ???
5???? CountSort
??????? ???
??
??????? ???
??
??????? ???
???? BucketSort ???? ????? ???? n ??????? ??????
????? "??? ???" 1..k, ????? ???? n ltlt k
6???? RadixSort
Least Significant Digits
321
321
1
111
321
123
231
1
123
123
231
111
1
132
231
132
132
2
213
132
213
312
2
213
213
312
332
3
223
312
111
123
2
231
111
223
213
3
312
223
332
223
3
321
332
213
213
4
331
213
7???? RadixSort
Radix-Sort(A,d) for i ? 1 to d do use BucketSort
to sort array A on digit i
RadixSort ???? ??????
??????? ???
??
????
RadixSort ???? ??????
8?????
???? BucketSort
??????? ???
?? ???? ???????
??????? ???
???? RadixSort
????
??????? ???
9????? 1
- ?????? n ?????? ????? ????? 1..nc ???? ???? ?????
???? O(nc) - ?????
- ????? ??????? ????? 1..n2 ??? ???? ????? ????
?O(n)? - ???? ?????????? ??? ?????? ???????? ???? ???????
- Counting sort ????? n ?????? ????? 0..k
- ????? ?? ???? ????? n, ??? ??????
- ????? ??? radix sort, O(n) ??? ???? ????
2O(n)O(n) - ???? ????? ??????? ??? ????? ???? ????
10????? ??????? ????????
- ??? ????? ????? ???? ???? ??????? ???? ?? ???.
???? ??????? ?????, ???????? ???????? - ??? ????? ??? ?????? ????? ????????? ???? ????
???. - ???? ??????? ????????? ?????? ???? ?? ????? ???
??? ???? O(t) - ??? O(t) ??? ??? ????? ???? ????? ????? ?? ?????
WC
11??? ???? ??? ???? ??????
- ???? ?????? ????? ???? ??????
- ???? ???????? ????? ?? ????? ??? ??? ????? n,
- ???? O(logn).
- ??? ????? ??? ????? ????? ?? ?????? ?
- ??? ???????? ????? ?? ????? ???? ??? ?????? ???
????? ??? ?(logn).
?? ?? ???? ???? ???????? ????? ?? ????? ???? O(1)
???? ????? ???????!
12???? ??? ???? ?????
- ??? ????? O(t) ???????? ?????? ????? ?? ?????
??? ??? ???? O(t) - ??? ????? ?(t)
- ????? ?? ???????? ??? ?????? ????? ?(t)
- ??????? ????? ??????? ????? ?? ??? ????? ?? ???
????? (???? ?? ????? ?????? ????? ????????). - ??? ????? ????. ???? ?? ???????? ?????? ?????
???????? ?-n ?????? ???? ????? ?? ?? ???? ????
???? ??? ?(n)
13??? ????? ?? ???? ?? ??? ???????
- ?? ???????? ????? ???? ????? ?? ?"? ???????, ???
??????? ??????? ?? ?-n ?????? - ?? n! ??????? ???????
- For lta,b,cgt
- a,b,c
- a,c,b
- b,a,c
- b,c,a
- c,a,b
- c,b,a
n!
14??? ????? ?? ???? ?? ??? ???????
- ???? ????? ?? ???????? ???? (???????) ?? ??? ??
?????? ?????? - ??? ???? ????? ????
- ??? ????? ??? ???????
- ??? ????
- ?? ???? ?????? ?? ?????? ????? ??? ????? ??? ??
????? ?? - ????? ??? ?? ?????? ????????
15??? ????? ?? ???? ?? ??? ???????
gt
gt
gt
gt
gt
16??? ????? ?? ???? ?? ??? ???????
- ??' ???????? ????????? ???? ???? ????? ?????
??? - ??' ???????? ?????? ???? ??? ?????
- ?? ????? ????? n ????? ??? ??? n! ???? ?????
- ?? ?????? ????? d ??? ??? 2d ???? ??? ?????
- ?? ?????? ??? 2d???? ???? ????? ??? ???? d
- ?? ??? n! ???? ???? ????? ????? ?? ?????
log(n!) - ???? ?? ???????? ???? ????? ??????? ???? ?????
log(n!) ??????? ????? ????? ?????.
17??? ????? ?? ???? ?? ??? ???????
18??? ????? ?? ?????
- ?? ???? ?????? ?? ????? ????? ??????
- ????? ????? ????? ????? ??? ????
- ?"? ??? ?? n ????
- ?? ????? ???????? ?? ?? ?????? ??? n ?????
- ??? ???????? O(logn)
19????? 2
- ??? "???" ????? ???? ??? ????? ???????
- ???? ????? ?????? ??? nlogn
- ????? ???????? ?? ??? ??????
- ???? ????? ????? ???? ??? ????? ?????? ?-f(n)
- ?????? n ??????, ????? ?? ??????? (????? ???????)
- ???? ???? ???? ?????? (O(n))
- ???? ?? ????? ??? ?????? ????? ???? f(n)
- ????? ?? ????? ?-in order ?????? ???? (???????)
???? ?????? - ??"? ??? O(n) f(n) O(n)
- ???, ?? f(n)ltltnlog(n) ???? ????? n ?????? ????
??? ?-O(nlogn) ???? ?????? ??? ???? ?????. ?.?.?
20????? 3
- ?????? ?? ?? ???? ????? ???????? ????? ????????
?? ???? ??? ????? n, ????? ??? ???????????
???????? ?? ??????? ?-1 ?? n ?????? ????? ????
???????.(???? ?????? ????? ?? ??????????,
????????? ???? ?????? ?? ???, ????? ??? ????).
21????? 3
- ?????? ?? ?? ???? ????? ???????? ????? ????????
?? ???? ??? ????? n, ????? ??? ???????????
???????? ?? ??????? ?-1 ?? n ?????? ????? ????
???????.(???? ?????? ????? ?? ??????????,
????????? ???? ?????? ?? ???, ????? ??? ????). - ?????
- ????? ????????, ?? ???? ???? ??? ???????????
(n!/2) ??? ?? ????? ????? ??? ???? - ??? ???? ??? ??? ?(nlogn) ??? ???? ????? ????????
??????
22????? 4
- ??? k ?????? ?????? (??????) ????? ????? ????? n
???? O(nlogk), ??? ???? ???? ??? ?????
1
2
3
4
5
6
7
8
x
....
?? ?? ???? ?-k
max-heap
????? ????? ?? ?? ????? ?-heap
23Selection
24????? 2
- ?????? ???? (?? ?????) ??? n ?????, ??? ?? k
?????? ?????? ????? ????? ???? O(n) - ?????
- ????? ???? ?????? ???? ???? ?-k ???? ????? (?"?
statistic order k), ??? ??? ???? ????? ?? ???
?????? ???????? - ??"?
- O(n) ????? ?? ???? ?k ??? ?????
- O(n) ????? ?? ????? ????? ?? ?? ?????? ????
25????? 3
- ??? ????? ????? ????? n/5 ?????, ????? ??????
???? ????? ????? - ???? ???? ????? ???? ?????
- ?? ?? ????? ??? ??????
- ??? ???? ?? ?????? ????
...
26????? 3
- ?????
- ???? ?? ?-order statistics ?????
- n/6
- 2n/6
- 3n/6
-
- ??? ??? ???? ????? ??????? ????? ?????? n/5 ?????
- ??"? ???????? - ???????
27????? 4
- ????? ????? ????? ???? ?????? ??????? ???? ???
????? ??????? - ?????
- ??? ???????
- ??? ?????????
- ???? ????? ?? ?? ??? ???, ???? ??"? ?? xlty
- ???? ???? ?????? ?? ???????
- ????? ??? ?? ????? ?x ??? ?? ???? ?y
- ????? ?? ?????? ?????????
A
B
?????
28????? 5
- ????? ??? ????? ????? n/2 ?????, ??? n/4 ?????,
??? n/8 ????? ???' - ???? ???? ????? ?? ????? ?????? ???????
- ?????
- ???? ????? ??? ???? ??????
- ?????? ?? ?? ?????? ??? ????, ???? ???????? ????
??????, ??????? ????????? ????? ??????? ?? logn
?????? - ???? ?????? ??? ???? ?????? ??????
29????