??? ?? - PowerPoint PPT Presentation

About This Presentation
Title:

??? ??

Description:

Title: Author: Jiang Yuan Last modified by: xiang chen Created Date: 8/20/2004 9:00:43 AM Document presentation format: (8.5x11 ) – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 63
Provided by: Jiang96
Category:
Tags: bubblesort

less

Transcript and Presenter's Notes

Title: ??? ??


1
??? ??
  • ??
  • ????
  • ????
  • ????
  • ????
  • ????
  • ???

2
9.1 ??
  • ?????????????????????????
  • ???(datalist) ???????????????
  • ???(key) ????????????, ?????????,
    ???????????????, ???????????????????????????????,?
    ???????????

3
  • ???????? ?????????? ???ri?rj, ?????? ki
    kj , ??????, ??ri??rj??????????,
    ??ri????rj???, ????????????, ???????????????
  • ??????? ??????????????????????????????????????
    ????,?????????,???????????,???????????????

4
  • ??????? ???????????????????????????????????????
    ???????????????
  • ???????????? ????????????
  • ????????????????????????????????????????????????,?
    ????????????????

5
9.2 ???? (Insert Sorting)
???? ???????????, ???????, ????????????????????
?, ???????????
1.?????? (Insert Sort)
  • ???? ????i (i ? 1) ????, ???V0, V1, ,
    Vi-1????????, ?Vi???????Vi-1, Vi-2,
    ??????????, ????????Vi??, ?????????????

6
??????
49
25
25
21
16
08
0 1 2 3 4 5
49
25
25
25
21
16
i 1
08
0 1 2 3 4 5
temp
49
49
25
25
21
16
i 2
08
0 1 2 3 4 5
temp
7
49
25
25
25
21
16
i 3
08
0 1 2 3 4 5
49
25
25
21
16
16
i 4
08
0 1 2 3 4 5
temp
49
25
25
21
16
i 5
08
08
0 1 2 3 4 5
temp
8
49
25
25
21
16
??
08
0 1 2 3 4 5
i 4 ??????
49
16
i 4 j 3
25
25
21
16
16
08
0 1 2 3 4 5
temp
49
49
i 4 j 2
16
25
25
21
16
08
0 1 2 3 4 5
temp
9
49
i 4 j 1
16
25
25
25
21
16
08
0 1 2 3 4 5
49
i 4 j 0
16
25
25
25
21
16
08
0 1 2 3 4 5
temp
49
i 4 j -1
16
25
25
21
21
16
08
0 1 2 3 4 5
temp
10
????????? template ltclass Typegt void dataList
ltTypegt InsertSort ( ) //???? Key
??????????? ElementltTypegt temp int i, j
for ( i 1 i lt CurrentSize i ) if
( Vectori lt Vectori-1 ) temp
Vectori for ( j i j gt 0 j-- )
//???????? if ( temp lt Vectorj-1
) Vectorj Vectorj-1
else break
11
????
Vectorj temp
  • ????????????????????????????
  • ?????, ????????????????, ???????1?, ??2???,
    ?????????? n-1, ???????2(n-1) ?

12
  • ?????, ????????KCN???????RMN???
  • ?????????????? O(n2)?
  • ?????????????????

13
2.?????? (Binary Insertsort)
  • ????? ???????? ????? V0, V1, , Vn-1???,
    V0, V1, , Vi-1 ?????????????Vi ?,
    ?????????Vi ??????
  • ?????????
  • template ltclass Typegt
  • void dataListltTypegt BineryInsSort ( )
  • ElementltTypegt temp int Left, Right
  • for ( int i 1 i lt CurrentSize i)
  • Left 0 Right i-1 temp Vectori

14
while ( Left lt Right ) int
middle ( Left Right )/2 if ( temp
lt Vectormiddle ) Right middle -
1 else Left middle 1
for ( int k i-1 k gt Left k-- )
Vectork1 Vectork VectorLeft
temp
15

????
  • ????????????, ?????????????????????????
  • ??????????????????????????, ????????????? i ????,
    ???? ?log2i? 1 ??????, ??????????????, ? n
    ???(?????, ?? n2k )???????????????????
  • ?????????????????

16
  • ??????? ? n ???, ????????????????????????,
    ??????????
  • ?????? ??????????????????????, ???????????

17
4.???? (Shell Sort)
  • ????????????????
  • ????
  • ????????? n ???, ??????? gap lt n ????,
    ??????gap??????????
  • ?????????
  • ?????? gap, ??? gap ?gap/2?,???????????????????
    gap 1, ???????????????????
  • ?99,14,28,31,2,7,46,70,62,180,30,82,170,5,9

18
49
25
25
21
16
08
0 1 2 3 4
5
i 1
49
Gap 3
25
25
21
16
08
49
25
25
21
16
08
49
25
25
21
16
08
19
49
25
25
21
16
08
0 1 2 3 4
5
i 2
49
Gap 2
25
25
21
16
08
49
25
25
21
16
08
49
25
25
21
16
08
20
49
25
25
21
16
08
0 1 2 3 4
5
i 3
49
Gap 1
25
25
21
16
08
  • ??? gap ????, ?????????, ?????? ??????, gap
    ?????, ????????????, ?????????, ??????????,
    ???????????

21
??????? template ltclass Typegt void
dataListltTypegt ShellSort ( )
ElementltTypegt temp int gap CurrentSize /
2 //gap?????? while ( gap ! 0 )
//??,??gap?? for ( int i gap i lt
CurrentSize i) temp
Vectori //?????? for ( int j i
j gt gap j - gap ) if ( temp lt
Vectorj-gap ) Vectorj
Vectorj-gap else break

22
Vectorj temp
gap ( int ) ( gap / 2 )
  • Gap????????? shell ??? gap ?n/2?,gap
    ?gap/2?,??gap 1?Knuth ??? gap ?gap/3?
    1????????????,?????? gap ?????
  • ???????????,???????????????????????

23
  • ?????????????????????(gap)?????????,??????????,???
    ??????
  • Knuth???????????? ? n ???,?????????????????????
    n1.25 ? 1.6n1.25 ?????????????????????????????????

24
9.3 ???? (Exchange Sort)
?????????????????,??????,??????????????????
1.???? (Bubble Sort)
  • ??????????????????? n???? n-1 ?,i 1, 2, ?, n-1
    ??? i ??????,j n-1, n-2, ?, i,??????Vj-1.key?
    Vj.key???????,???Vj-1?Vj?

25
49
25
25
21
16
08
0 1 2 3 4
5
49
25
25
i 1
21
16
08
Exchang1
49
25
25
i 2
21
16
08
Exchang1
49
25
25
i 3
21
16
08
Exchang1
26
49
25
i 4
25
21
16
08
Exchang0
0 1 2 3 4
5
??????? template ltclass Typegt void
dataListltTypegt BubbleSort ( ) int pass
1 int exchange 1 while ( pass lt
CurrentSize exchange ) exchange
0 //????0,????? for ( int j
CurrentSize-1 j gt pass j-- ) if (
Vectorj-1 gt Vectorj ) //??
27
Swap ( Vectorj-1, Vectorj ) //??
exchange 1 //????1,???
pass
  • ?i?????????Vi-1,Vi,?,Vn-1????,
    ??????????????????????????(i-1), ?????????????????

28
  • ???n-1??????????????
  • ???????????????????????????,??????????,?n-1??????
    ,??????
  • ????? ????n-1???,?i? (1? i? n) ? n- i ??????, ??
    n-i ???????????????????????KCN???????RMN?

29
  • ??????????????????????
  • ???????????????

30
2.???? (Quick Sort)
  • ????
  • ??????????????? (????????) ????, ???????????,
    ?????????????????????????????????????????????
    ???????????????????????????
  • ???????????????(??????????????)?
  • ????????????????,??????????????????

31
????
QuickSort ( List ) if ( List?????1)
???List???????? LeftList ?
Right List QuickSort ( LeftList
) QuickSort ( RightList ) ??????
LeftList ? RightList ???????List

32
  • ?????????
  • ???
  • ? (?)
  • ??(??)

33
  • ?????
  • 28 19 27 48 56 12 10 25 20 50
  • ??????
  • 20 19 27 25 10 12 28 56 48 50
  • ?????
  • 12 19 10 20 25 27 28 50 48 56
  • ?????
  • 10 12 19 20 25 27 28 50 48 56

34
??????? template ltclass Typegt void
dataListltTypegt QuickSort ( const int left,
const int right ) //??? left?right ??????????
if ( left lt right) int pivotpos
Partition ( left, right ) //??
//???????? QuickSort ( left,
pivotpos-1) //????????
QuickSort ( pivotpos1, right )
35
int dataListltTypegt Partition (int s, int t
) int x, i, j xVectors is
jt while ( i lt j) while ( ( i
lt j) (Vectorjgtx) jj-1 if
(iltj) aiaj while ( ( i lt j)
(Vectoriltx) ii1 if (iltj)
ajai Vectori x return i
36
????
  • ?????????????, ?????????????????
  • ?????? ????????????,??????????
  • ???? ?log2(n1)? ???,??????? O(log2n)?
  • ????? ?????????????????????????, ?????????,
    ???????????????????????????n-1 ??????????,???????
    O(n)?

37
  • ????
  • ???? O(nlog2n)
  • ????? O(n2)
  • ????????????, ??? i ????? n-i ??????????? i
    ????????,????????????
  • ????????????????
  • ?? n ?????????, ????????, ??? n ???,
    ?????????????????????

38
9.4 ????
???? ??? (??? i ?, i 0, 1, , n-2) ??? n-i
?????????????????, ?????????? i ??????? n-2 ???,
????????1?, ???????
39
1.?????? (Select Sort)
  • ????????????????, ???????
  • ????? ViVn-1 ?????????????
  • ???????????????, ?????????????????
  • ???????????????????????????Vi1Vn-1?????????
    ?, ?????????????

40
49
25
25
??
21
16
08
0 1 2 3 4
5
??? 08 ??21,08
49
25
25
i 0
21
16
08
??? 16 ??25,16
49
25
25
i 1
21
16
08
??? 21 ??49,21
49
25
25
i 2
21
16
08
41
??? 25 ???
49
25
25
i 3
21
16
08
0 1 2 3 4
5
??? 25 ???
49
25
25
i 4
21
16
08
49
25
25
??
21
16
08
????????
42
????????? template ltclass Typegt void dataList
ltTypegt SelectSort ( ) for ( int i 0 i
lt CurrentSize-1 i ) int k i
//??????? for ( int j i1 j lt
CurrentSize j) if ( Vectorj lt
Vectork ) k j
//??????????? if ( k ! i ) //???? i
??? Swap ( Vectori, Vectork )
43
3.??? (Heap Sort)
  • ???????, ????????????????
  • ?????????
  • ????????,???????? FilterDown( ) ?????
  • ?????????????????????

44
????????
0
0
i
i
21
21
1
1
2
2
25
49
25
49
3
4
5
5
4
3
25
16
08
25
16
08
21 25 49 25 16 08
21 25 49 25 16 08
???????
i 2 ??????
45
i
0
0
21
49
1
1
2
2
25
49
25
21
3
4
5
5
4
3
25
16
08
25
16
08
21 25 49 25 16 08
49 25 21 25 16 08
i 1 ??????
i 0 ?????? ?????
46
??????????
template ltclass Typegt void MaxHeapltTypegt
FilterDown ( const int i, const int EndOfHeap
) int current i int child 2i1
Type temp heapi while ( child lt
EndOfHeap ) //???? if ( child 1 lt
EndOfHeap heapchild lt heapchild1 )
child child1 //???????
if ( temp gt heapchild ) break
//temp????, ???? else
47
heapcurrent heapchild //?????
current child
//?????? child 2child1
heapcurrent temp //???????
?????? for ( int i ( CurrentSize-2) / 2 i
gt 0 i-- ) FilterDown ( i, CurrentSize-1 )
48
??????????
  • ?????V0????????, ?V0? Vn-1??,
    ????????????????, ?????n-1???,
    ????????FilterDown(0, n-2), ???????,
    ???????????????V0???
  • ???V0?Vn-2,??FilterDown(0, n-3),
    ??n-2???????,?
  • ??????,???????????????

49
0
0
49
08
1
1
2
2
25
21
25
21
3
4
5
5
4
3
25
16
08
25
16
49
49 25 21 25 16 08
08 25 21 25 16 49
?????
?? 0 ?? 5 ???, 5 ?????
50
0
0
25
16
1
1
2
2
25
21
25
21
3
4
5
5
4
3
08
16
49
08
25
49
25 25 21 08 16 49
16 25 21 08 25 49
? 0 ?? 4 ? ?? ??????
?? 0 ?? 4 ???, 4 ?????
51
0
0
25
08
1
1
2
2
16
21
16
21
3
4
5
5
4
3
08
25
49
25
25
49
25 16 21 08 25 49
08 16 21 25 25 49
? 0 ?? 3 ? ?? ??????
?? 0 ?? 3 ???, 3 ?????
52
0
0
21
08
1
1
2
2
16
08
16
21
3
4
5
5
4
3
25
25
49
25
25
49
21 16 08 25 25 49
08 16 21 25 25 49
? 0 ?? 2 ? ?? ??????
?? 0 ?? 2 ???, 2 ?????
53
0
0
16
08
1
1
2
2
08
21
16
21
3
4
5
5
4
3
25
25
49
25
25
49
16 08 21 25 25 49
08 16 21 25 25 49
? 0 ?? 1 ? ?? ??????
?? 0 ?? 1 ???, 1 ?????
54
?????? template ltclass Typegt void MaxHeapltTypegt
HeapSort ( ) //??heap0?heapn-1????,
???? //??????????????? for ( int i (
CurrentSize-2 ) / 2 i gt 0 i-- )
FilterDown ( i, CurrentSize-1 ) //???
for ( i CurrentSize-1 i gt 1 i--)
Swap ( heap0, heapi ) //??
FilterDown ( 0, i-1 ) //?????
55
????
  • ??????????O(nlog2n)?
  • ???????????for????????????????????????,??????????O
    (1)?
  • ???????????????

56
9.4 ???? (Merge Sort)
  • ?? ????????????????????????
  • ????initList??????Vl Vm?Vm1
    Vn????????????, ????????mergedList?Vl Vn
    ??
  • ???????????? (2-way merging)?
  • ??? i ? j ????Vl Vm?Vm1 Vn?????????? k
    ??????

57
left mid
mid1 right
initList
08 21 25 25 49 62 72 93
16 37 54
i j
left
right
mergeList
08 16 21 25 25 37 49 54 62 72 93
k
  • ? i ? j ????????????, ????????????,
    ??????????????? k ?????
  • ? i ? j ???????????,??? ???????????????

58
1.?????????
  • ????
  • ????????? n ???,??????? n ???? 1 ??????
    (???),??????,?? ?n / 2? ???? 2 ???? (?? n
    ???,??????????????1)??????,,????,????????? n
    ??????

59
?????? template ltclass Typegt void dataListltTypegt
merge ( dataList ltTypegt mergedList, const
int left, const int mid, const int
right ) int i left, j mid1, k
left while ( i lt mid j lt right )
//???? if ( Vectori lt Vectorj )
mergedList.Vectork Vectori
i k else
mergedList.Vectork Vectorj
60
j k if ( i lt
mid ) for ( int n1 k, n2 i n2 lt
mid n1, n2 )
mergedList.Vectorn1 Vectorn2 else
for ( int n1 k, n2 j n2 lt right
n1,
n2) mergedList.Vectorn1 Vectorn2
61
?????????
  • ?initList.Vector0?initList.Vectorn-1? n
    ???????????? len ????, ??????????, ?????? 2len
    ????, ????mergedList.Vector??
  • ??n??2len????, ????????,????????
  • ???????len????????????len????,
    ??merge?????????????? 2len ?????
  • ????????,???????? len, ??????MergedList.Vector??

62
template ltclass Typegt void datalistltTypegt
MergePass ( dataListltTypegt mergedList,
const int len ) int i 0 while
(i2len lt CurrentSize-1)
merge(mergedList, i, ilen-1, i2len-1)
i 2 len //??????
if ( ilen lt CurrentSize-1 )
merge(mergedList, i, ilen-1, CurrentSize-1)
else for ( int j i j lt CurrentSize-1 j)
mergedList.Vectorj Vectorj
63
?????????
len1
21
25
25
93
62
72
08
37
16
54
49
len2
25
21
25
49
62
93
08
72
16
37
54
len4
21
25
25
49
08
62
72
93
16
37
54
len8
08
21
25
25
49
62
72
93
16
37
54
len16
08
16
21
25
25
37
49
54
62
72
93
64
(??)???????? template ltclass Typegt void
dataListltTypegt MergeSort ( )
//??????????????????? dataListltTypegt
tempList ( MaxSize ) int len 1 while
( len lt CurrentSize ) MergePass (
tempList, len ) len 2 tempList.MergePass
( this, len ) len 2
65
  • ???????????, ??MergePass( ) ?????????, ???merge (
    )?? ?n/(2len)? ? O(n/len) ?, ??MergeSort(
    )??MergePass( )???log2n? ?,???merge(
    )?????O(len)?, ????????????O(nlog2n)?
  • ????????????, ???????????????????????????????????
  • ???????????????
Write a Comment
User Comments (0)
About PowerShow.com