k,a,l,c,d,s - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

k,a,l,c,d,s

Description:

Title: 4 Author: kusakari Last modified by: kusakari Created Date: 4/2/2002 12:13:55 AM Document presentation format – PowerPoint PPT presentation

Number of Views:8
Avg rating:3.0/5.0
Slides: 72
Provided by: kus85
Category:
Tags: heap | sort

less

Transcript and Presenter's Notes

Title: k,a,l,c,d,s


1
4.?????????
2
??(???)
???
???
k,a,l,c,d,s
5,3,8,1,6,21,11
?????????
?????????
1,3,5,6,8,11,21
a,c,d,k,l.s
3
?????????
CPU
CPU
??????
??????
??????
????
???
???
??????
????
????
????
????
4
?????
????
????????????????????? ??????????????
(????RAM????????????????)
????
CPU-??????????????? ????-??????????????????? ?????
????????????? (?????????????????????????? ???)
5
????????????
??????
?????
?????
??????
??????
???????
??????
???????
?????
6
????????????
??
?????
???????
??????
O(n2)
?????
???????
???(??)
?????
?????
???????
????O(n)
??????
O(n log n)
???????
??????
7
?????
2
5
3
8
1
4
13
9
??
??A
A0
A1
An-1
Ai
?
n
5
3
8
4
13
9
2
1
?? (????)
??A
A0
A1
An-1
?
n
?????????????????????? ????????????????????
8
??????
??
??????????????(???????)? ????????
??????????????????????
???????????????????
9
?????????1
A
5
1
5
5
5
5
5
0
5
5
3
3
3
3
3
1
3
1
3
3
2
8
8
8
8
1
8
8
8
1
1
1
1
8
1
3
2
2
4
4
2
2
2
4
4
???
4
4
4
4
4
5
13
2
13
13
13
2
13
13
13
13
6
9
??
9
9
9
9
9
9
9
7
2
10
?????????2
?????
A
1
1
1
1
1
0
1
1
1
5
2
2
2
2
2
1
2
2
????????
5
2
3
3
3
3
3
3
3
3
5
4
4
4
8
4
4
3
8
4
5
5
5
4
2
5
5
4
5
4
8
8
8
8
8
8
9
9
9
9
9
9
9
6
13
13
13
13
13
13
7
9
13
13
11
?????????1
/?????? Ai?Aj??????/ void swap(int i ,int
j, datatype A) datatype temp /
?????????/ tempAi
AiAj Ajtemp return
12
?????????2
/ ??????/ void bubble(datatype A) int
i,j / ????/ for(i0iltni)
for(jn-1jgtij--)
if(Aj-1gtAj)
swap(j-1,j,A)

return
13
??????????
1???????????n-1??????? 2??
n-2 n-1???????????1???????
????
???
???????
14
?????
??
?????????????????????? (???????????????)
???????????????????????
???????????????????
?????
?????????????
15
????????1(?????)
????
A
0
5
5
5
5
5
5
5
5
1
3
1
3
3
3
3
3
3
3
3
2
8
8
8
8
8
8
8
8
8
???
1
3
1
1
1
1
1
1
1
5
4
4
4
4
4
4
4
4
4
4
5
13
13
13
13
13
13
13
13
13
6
9
9
9
9
9
9
9
9
9
7
2
2
2
2
2
2
2
2
2
min0
min1
min1
min3
min3
min3
min3
min3
16
????????2
A
?
1
1
1
1
0
1
1
1
1
2
2
2
2
2
3
2
2
1
3
3
3
3
2
3
8
8
3
?????
4
4
4
4
4
5
3
5
5
5
5
5
5
5
4
4
4
4
13
13
8
8
5
8
13
13
13
9
9
9
9
9
9
6
9
9
8
8
13
13
3
8
13
2
7
min4
min7
min6
min7
min7
min7
min4
17
????????1(?????????????)
/???????Ai??Aj???????????/ int
select_min(int i ,int j, datatype A) int
k / ???? / int min /
????????/ mini for(kikltjk)
if(amingtak)mink
return k
18
????????2
/ ?????/ void select(datatype A) int
i / ????/ int min / ??????/
for(i0iltni)
minselect_min(i,n-1,A)
swap(i,min,A) return
????????????select_min?????? ????????????????????
???2?????????????????? (?????????????)
19
?????????
1???select_min??n-1???? 2??
n-2 n-1???select_min??1????
????
????
????n?
???
???????
20
?????
??
????????????????????
?????????????????????? ????????1??????
???????????????????
???????
?????
21
????????
????
A
0
5
3
3
1
1
1
1
1
3
5
5
3
1
3
3
3
2
2
5
4
8
8
8
4
4
3
????
1
1
1
8
5
3
5
5
4
8
8
8
4
4
4
4
4
5
5
9
8
13
13
13
13
13
13
13
6
9
9
9
9
9
9
9
2
7
2
2
2
2
2
2
13
22
????????1(????????)
/???????????? A0??Ai-1??????????? Ai???????
?/ int find_pos(int i ,datatype A) int
j / ???? / for(j0jltij)
if(AjgtAi)break
return j
23
????????2
/ ?????/ void insert(datatype A) int
i,j / ????/ int pos / ???????/
for(i0iltni)
posfind_pos(i,A)
for(jn-1jltposj--)
swap(j-1,j,A)
return
24
?????????
n-1???????????n-1?????????? n-2??
n-2?? 1??
1??????????
????
????????????
???
???????
(?????????????????????????????????)
25
??????
??
?????????????
???????????????????????????
????????????????????
0
13
2
1
4
9
4
5
3
2
6
3
8
5
1
7
26
???????????1
??
???
1
2
3
4
5
7
6
0
5
13
A
3
8
1
4
9
2
0
5
5
13
3
8
1
4
9
2
???
????
0
5
5
13
3
8
1
4
9
2
1
???
????
3
27
???????????2
??
???
1
2
3
4
5
7
6
0
0
8
13
A
5
3
8
1
4
9
2
2
1
3
5
???
????
??????
1
2
3
4
5
7
6
0
A
5
3
8
1
4
13
9
2
7
???
28
???????????1
0
13
2
1
2
3
4
5
7
6
0
1
9
4
A
5
3
8
4
13
9
2
1
4
5
3
2
6
3
5
8
???
7
1
?????????????? ?????
0
9
2
1
2
3
4
5
7
6
0
1
4
8
9
A
5
3
8
1
4
13
2
4
5
3
2
6
3
5
1
???
????
29
???????????2
1
2
3
4
5
7
6
0
0
8
8
2
A
5
3
1
4
13
9
2
1
5
4
???
????
4
5
3
2
3
1
??????
3
8
A
5
1
4
13
9
2
????
30
?????????
void heap_sort(datatype A) int i
/ ???? / datatype max /???/ /
???? / for(i0iltni)insert_heap(Ai,
A) / ???????????/
for(in-1igt0i--)
maxdelete_max(A) Aimax
return
31
??????????
???????
??insert_heap(A) 1????????
n?????????
???????????
??delete_max(A) 1????????
n?????????
?????1?????????
???
???????
(????????????
??????????????????)
32
??????
??
????????????????????(?????)
???????????????????
????????2????????? ??????????????? ?????????????(?
????)
B
5
3
8
1
3
8
A
5
1
4
13
9
2
4
13
9
2
C
33
??????
B
5
3
8
1
A
4
13
9
2
C
B
5
3
8
1
A
1
4
13
9
2
C
?????
B
5
3
8
1
A
1
2
4
13
9
2
C
34
??
??2???????????? ???????????????????????? ?????
??????2????????????
??????? ?????????????????? ?????
???????????????????
35
??????????(??)
3
1
2
4
5
7
6
0
13
A
5
8
9
3
1
4
2
A0??A3?? ??????
A4??A7?? ??????
1
2
3
0
4
5
7
6
13
5
8
3
1
9
4
2
m_sort(0,1,A)
m_sort(2,3,A)
1
0
2
3
7
6
4
5
5
3
13
9
2
8
1
4
6
1
3
0
2
5
7
4
13
5
3
8
1
4
9
2
36
??????????(???)
1
2
3
4
0
5
7
6
13
5
3
8
1
4
9
2
marge
6
4
5
7
2
3
0
1
7
6
13
9
2
4
8
1
5
3
1
2
3
0
4
5
7
6
8
5
3
1
9
4
13
2
5
0
1
2
3
4
7
6
A
9
5
8
3
1
4
13
2
37
??????
/ ??B???C???A????????????????????/ void
marge(int posA,datatype A,datatype B ,int
numB,datatype C,int numC) int
iA0,iB0,iC0 / ????/
while(iAltnumBnumC-1)
if(BiBltCiC)
AposAiABiB
iB
else

AposAiACiC
iC
iA return
38
?????????
/?????????????/ void marge_sort(int left,int
right,datatype A) int mid /
????? / datatype Bn,Cn /??????/
if(leftright)return
mid(leftright)/2 marge_sort(left,mid,A)
marge_sort(mid,right,A)
/????Aleft??Amid???B??
Amid??Aright???C???????????? /
marge(left,A,mid-left,B,right-mid,C)
return
39
??????????
??????????????? ??????????
?????????? ??????
????????????????????????????
?????
???????????
?????????????
???????
40
???????
??
????????????????????(?????)
?????????(????)?????? ???????????????????
???????????? ???????????????????
1
2
3
4
5
7
6
0
5
13
A
3
8
1
4
9
2
????
4
13
A
8
9
2
5
1
3
???
???
41
??????
?????????????? ??????
????????????????? ???????????????????? (??????????
?????) ???????????????????????? ??????
???? ????????????? ?????????????????
42
????????????(??)
5
13
A
3
8
1
4
9
2
????????? ????
?????? ???????
5
13
A
3
8
1
4
9
2
???????????
???????????????????????????
5
13
A
3
8
1
4
9
2
43
????????????(??)
1
2
3
4
5
7
6
0
5
13
A
3
8
1
4
9
2
A0??A4??? ?????
A5??A7??? ?????
q_sort(0,3,A)
1
2
3
4
0
5
7
6
5
A
3
1
4
2
13
8
9
????
3
1
2
0
5
6
7
A
3
1
2
4
13
8
9
????
44
??????????1(??)
/?????????????/ int partition(int left,int
right,datatype A) int i,j
/????/ ileft1 jright
for() while(AiltAleft)
i while(AjgtAleft)j--
if(igtj)break swap(i,j,A)
return(i)
45
??????????2(??)
/?????????????/ void quick_sort(int left,int
right,datatype A) int pos
/???? / pospartition(left,right,A)
swap(left,pos,A) quick_sort(left,pos-1,A)
quick_sort(pos1,right,A)
return
46
???????????
????????? ????????????????????? ????? ????????????
???? ??????????????????????????? ????????????????
??????????
47
?????????????
?????partition(i,j,A)?1??????? j-i1??????????? ??
???????parttition()????? ?????
??????
???0????????????? ???????????????
????????????????????????????????? (??????????????)
???????partition(i,j,A)??????j-i1?????????? ????
???????????????????
?????
??????????
48
?????????????
??????????? ? ?????
????? ???????? ???????????
???????? ??????????
???????? ??????????
partition()?
?????????n????????????? ?????????n????????????
2?????????????????
49
???????(??????????????????)
??A
A0
A1
An-1
Ai
Aj
??? ????? ??? ??? ??
Aj
Ai
?????
50
?????????????2
??????????????? ??????????????
data1
data2
??A
A0
A1
An-1
Ai
0
1
i
n-1
??B
B0
B1
Bn-1
Bi
?????B??????????A???????
swap(0,i)
(data1????????)
data2
data1
??A
A0
A1
An-1
Ai
1
0
i
n-1
??B
B0
B1
Bn-1
Bi
51
?????????????3
????
data1
data2
??A
A0
A1
An-1
Ai
data1
data2
??A
A0
A1
An-1
Ai
????????????????? (??????????????????????????????)
52
?????????(??p.18??)
????????????????? ??????(????????)???????????
??????????????
???????????????????????
????? ????
????????
????? ????
????????
????? ????
53
?????
???????????????????????? ?????????????????????????
?? ???1????????????????????????? ?????????????????

??????????????
????? ????
?
????? ????
?
??????? ??????
???????????????????
54
??????????(????????????????)
????????? ???????????????? ?????
????????????????? ?????????
????
????
??????????????????? ??????????? ??????? ??????????
????? ?????????????????? ????
????
55
3????????????
a b c a c b b a c b c a c a b c b a
??????
Aa b c
??A???
1true
0false
A1ltA2? (bltc?)
a c b c a b c b a
Aa c b
a b c b a c b c a
Aa b c
1
0
1
0
A0ltA1? (altc?)
A0ltA1? (bltc?)
a c b
c a b c b a
a b c
b a c b c a
Ac a b
Ab a c
Aa c b
Aa b c
0
1
1
0
A1ltA2? (altc?)
A1ltA2? (altc?)
b c a
b a c
c a b
c b a
Ab c a
Ab a c
Ac a b
Ac b a
56
????????
??????????????????? ????n!??????????????? ????????
????????? ???????????
???????????????????? ???????????????
???h????????? ??? ?????
????
????
????????????????
???????????
57
?????????
????? ????
????????
???????? (?????? ???????)
???????????? ???????????
58
??????????
???????
?????????????????????? ???????????????????? ??????
????????????????
?????
??????????? ???????????????????
59
???????
????????????? ????? 1.?????? 2.
0??m-1??? ????????????????? (??????????2????)
??
m??????(??)?????? ????????????
???????????????????
60
??????????1
2
4
0
3
1
6
??A
A0
A1
An-1
Ai
-1
-1
-1
-1
-1
-1
-1
-1
??B
B0
B1
Bm-1
B3
B2
1
6
2
4
0
3
??A
A0
A1
An-1
Ai
-1
-1
-1
-1
-1
-1
-1
3
??B
B0
B1
Bm-1
B3
B2
6
2
4
0
1
3
??A
A0
A1
An-1
Ai
-1
-1
-1
-1
-1
-1
6
3
??B
B0
B1
Bm-1
B3
B2
B6
61
??????????
/????????????? ???????????int????????/ void
bucket_sort(int A,int B) int i
/????/ for(i0iltni)
BAiAi return
62
??????????2(????????)
2
4
0
3
1
6
??A
A0
A1
An-1
Ai
-1
-1
-1
-1
-1
-1
-1
-1
??B
B0
B1
Bm-1
B3
B2
3
1
6
2
4
0
??A
A0
A1
An-1
Ai
-1
-1
-1
-1
-1
-1
-1
0
??B
B0
B1
Bm-1
B3
B2
3
6
2
4
0
1
??A
A0
A1
An-1
Ai
-1
-1
-1
-1
-1
-1
3
1
??B
B0
B1
Bm-1
B3
B2
B6
63
??????????2
/ ?????????????????/ void bucket_sort(int
A,int B) int i /????/
for(i0iltni)
BAii return
i???????????ABi????
64
???????(????)
??
???????????????? (??????????)
?????????????? ??????????????????? (??????????????
???????)
65
??????????3(????)
A
B
C
0
2
0
6
0
3
0
1
4
1
2
1
0
2
1
2
5
2
4
0
1
3
0
3
3
2
4
2
4
1
4
2
5
2
7
5
2
5
6
0
4
6
7
5
7
5
??????????? ?????????????
66
??????????3(????)
/ ????????????/ void bucket_sort(int A,struct
cell B,int C) int i,j
/????/ /??B???/ for(i0iltni)enqu
e(BAi,i) /???????C???/
i0 for(j0jltmj)
while(Bj?????)
CiAdeque(Bj) i
return
67
???????????
??1???????????????????? (RAM??????????????????? ??
??????) ????????????????n???? ??? ??B????????????
?????????
?????
??????????
68
?????
??
??????????? ???????????????????
?????????????????
69
????????(3?)
A
A
A
A
0
0
650
0
2
0
2
221
250
650
106
23
1
1
1
1
215
2
23
2
221
2
2
33
3
2
3
2
3
221
3
47
0?? ???
23
4
106
4
372
4
4
106
1?? ???
2?? ???
226
5
226
5
23
5
5
126
6
250
6
33
6
126
6
215
7
126
7
215
7
33
7
221
372
106
47
226
8
8
8
8
47
226
650
250
9
9
9
9
215
126
250
372
10
10
10
10
372
33
47
650
11
11
11
11
70
????????
/????????/ void radix_sort(int A) int
i,j /????/ for(k0kltmax_kk)
bbucket_sort(A,k)
/?????????k????????
????????????????/
return
71
?????????
??????????????????? k?????????????
??????????
?????
???
??????
??????????????
??????
??
????????????????
??????????????????? ?????m????k?????????
Write a Comment
User Comments (0)
About PowerShow.com