Title: Sort(??)
1Sort(??)
2Outline
- Sort
- Internal Sorting
- Bubble Sort
- Selection Sort
- Insertion Sort
- Quick Sort
- Heap Sort
- External Sorting
- Merge Sort
3Sort
- ???????????????,???????????,????????(???)????????
- ??????????
- Internal Sorting(????)
- ???????????,???????
- External Sorting(????)
- ??????????????,??????????? ????
4Internal Sorting---Bubble Sort
- ????????????,??????????????
- ??
- ????,??????,??????????,?????,?????
- ?????????????????,
- 26, 5, 81, 7, 63
- Step1 5, 26, 7, 63, 81
- Step2 5, 7, 26, 63, 81
- Step3 5, 7, 26, 63, 81
- Step4 5, 7, 26, 63, 81
- ??????,???????????,why?
- ????????????4???
5Internal Sorting---Bubble Sort Example
- ?????????(?????).
- void main( )
-
- int i,j,tmp
- int a5 26, 5, 81, 7, 63
- for ( i 0 i lt 4 i )
-
- for ( j 0 j lt 4 j )
-
- if (aj gt aj1)
-
- tmp aj
- aj aj1
- aj1 tmp
-
-
- printf(Loop d ,i)
- for ( j 0 j lt 5 j )
- ?????O(n2), why?
- ???????????????,??n???,????n-1???????
- ????????????,???(n-1) ?(n-2)??2?1,???n(n-1)/2????
???.
6Internal Sorting---Bubble Sort Example
a0 a1 a2 a3 a4
a0 a1 a2 a3 a4
i0j0 j1 j2 j3
i1j0 j1 j2 j3
5 26 81 7 63
5 26 7 63 81
5 26 81 7 63
5 7 26 63 81
5 26 7 81 63
5 7 26 63 81
5 26 7 63 81
5 7 26 63 81
a0 a1 a2 a3 a4
a0 a1 a2 a3 a4
i3 j0 j1 j2 j3
i2 j0 j1 j2 j3
5 7 26 63 81
5 7 26 63 81
5 7 26 63 81
5 7 26 63 81
5 7 26 63 81
5 7 26 63 81
5 7 26 63 81
5 7 26 63 81
7Internal Sorting---Selection Sort
- ??????????????????????????????????????????????????
,??????,???????????
8Internal Sorting---Selection Sort
- ??????????????????
- l, k, j, o, a, b
- ??????,??????????,?????????????.
- ?????O(n2),???????Bubble Sort??.
l k j o a b
a k j o l b
????? ?????????? ??????????
a b j o l k
a b j o l k
a b j k l o
a b j k l o
9Selection Sort Example
- void select(char string,int count)
-
- int pos /???????/
- int i,j
- char temp
- for ( i 0 i lt count - 1 i )
/?????,????/ -
- pos i
- temp stringpos
- for ( j i 1 j lt count j )
/?????,???????/ - if ( stringj lt temp ) /????/
-
- pos j /??????/
- temp stringj
-
- stringpos stringi /?????/
- stringi temp
- printf("???? s\n",string) /??????? /
-
10Internal Sorting---Insertion Sort
- ?????????????????
- ?????????,??????????????????,???????????????
- ??????????????
- ??
- ?????????,?????????????
- ??????????????
- ???????,????????????
11Internal Sorting---Insertion Sort
- ?????O(n2),why?
- ????????n-1???,????????1,2,3n-2,n-1?,???n(n-1)/2.
- ??????????????????
- l, k, j, o, a, b
???? ???????????????
l k j o a b
k l j o a b
j k l o a b
j k l o a b
a j k l o b
a b j k l o
12Insertion Sort Example
- include ltstdlib.hgt
- define MAX 20 /??????/
- void insert(char string,int count)
-
- int i,j
- char temp
- for ( i 1 i lt count i ) /
?????,????????/ -
- temp stringi /????/
- j i - 1 /????/
- /??????,???????????/
- while ( j gt 0 temp lt stringj )
-
- stringj1 stringj
- j--
-
- stringj1 temp /????/
- printf("???? s\n",string) /???????/
-
13Quick Sort(????)
- ?????
- ???????????,????????
- ?????????????,???????????????
- ???????????????,??????????
- ????????????,??????,?????????
- ??????????.
- ????????????.
14Quick Sort
d e a c f b h g
0 1 2 3 4 5 6 7
d e a c f b h g
??1,5
i
j
d b a c f e h g
??i??????j??,???????j??
j
i
c b a d f e h g
???????????
15Quick Sort
c b a
??????????
a b c
?????????
e f h g
?????
g h
a b c d e f g h
16Quick Sort
- void q_sort(char string,int left,int right)
-
- char partition /????/
- char temp
- int i,j,k
- if ( left lt right ) /??????/
-
- i left /?????/
- j right 1 /?????/
- partition stringleft /??????/
- do
- do /?????,??????par???/
- i
- while( stringi lt partition )
- do /?????,??????par???/
- j--
- while( stringj gt partition )
- if ( i lt j )
- temp stringleft /?igtj ???j?left???/
- stringleft stringj
- stringj temp
- printf("???? ")
- for ( k left k lt right k) /??????
/ - printf("c",stringk)
- printf("\n") /??/
- q_sort(string,left,j-1) /??????????/
- q_sort(string,j1,right)/??????????/
-
17Quick Sort
- void quick(char string,int n)
-
- q_sort(string,0,n-1)
-
- void main()
-
- char stringMAX /????/
- int count /????/
- printf("???????? gt ")
- gets(string) /????/
- count strlen(string) /??????/
- quick(string,count) /?????/
- /???????/
- printf("\n?????? s\n",string)
18Heap Sort
- Heap????????,????????
- ?????????,????????????
- Heap????
- Step1??????heap.
- Step2??heap???,????????????heap.
- Step3??????????????.
19Heap Sort
(1)
5
(2)
(3)
6
4
(4)
(5)
(6)
(7)
8
2
7
3
(8)
(9)
1
9
????1??, ??n??????2n,?????2n1.
1 2 3 4 5 6 7 8 9
5 6 4 8 2 3 7 1 9
20Heap Sort---??
- ???????.
- ?????n???,???????n/2.
- ????????????????,????????,??????????.
- ??????????
- Step1???????,??????????????????.
- (1)???????,????.
- (2)????????,?????????,????????????Step1.
21(1)
(1)???????????4(9/2).??????????,?????.
5
(2)
(3)
6
4
(4)
(5)
(6)
(7)
8
2
7
3
(8)
(9)
1
9
(1)
(2)?????9???????,????.
5
(2)
(3)
6
4
(4)
(5)
(6)
(7)
9
2
7
3
(8)
(9)
1
8
22(1)
(3)???????3???.???????,?????.
5
(2)
(3)
6
4
(4)
(5)
(6)
(7)
9
2
7
3
(8)
(9)
1
8
(1)
(4)?????7???????,????.
5
(2)
(3)
6
7
(4)
(5)
(6)
(7)
9
2
4
3
(8)
(9)
1
8
23(1)
(5)???????2???.???????,?????.
5
(2)
(3)
6
7
(4)
(5)
(6)
(7)
9
2
4
3
(8)
(9)
1
8
(1)
(6)?????4???????,??????,??????8???,???.
5
(2)
(3)
9
7
(4)
(5)
(6)
(7)
6
2
4
3
(8)
(9)
1
8
24(1)
(7)???????1???.???????,?????.
5
(2)
(3)
9
7
(4)
(5)
(6)
(7)
8
2
4
3
(8)
(9)
1
6
(1)
(8)?????2???????,?????????,????????,???.
9
(2)
(3)
5
7
(4)
(5)
(6)
(7)
8
2
4
3
(8)
(9)
1
6
25(1)
(9)???????4???.???????,?????.
9
(2)
(3)
8
7
(4)
(5)
(6)
(7)
5
2
4
3
(8)
(9)
1
6
(1)
(10)?????9????????,??????.
9
(2)
(3)
8
7
(4)
(5)
(6)
(7)
6
2
4
3
(8)
(9)
1
5
26Heap Sort
- include ltstdlib.hgt
- / ???? /
- void adjust_heap(int heap,int root,int len)
-
- int done / ???????? /
- int j
- int temp
- j 2 root / ????? /
- temp heaproot / ????? /
- done 0 / ???? /
- while ( j lt len !done ) / ??? /
-
- if ( j lt len ) /
??,????????????/ - if ( heapj lt heapj1 )
- j / ???? /
- if ( temp gt heapj ) / ????? /
- done 1 / ?? /
- else
- / ???? /
- void heap(int heap,int len)
-
- int i,j,temp
- for ( i ( len / 2 ) i gt 1 i-- )
/????????/ - adjust_heap(heap,i,len)
- printf("\n????? ")
- for ( j 1 j lt 10 j ) /??????? /
- printf("d",heapj)
- printf("\n") /?? /
- for ( i len - 1 i gt 1 i-- ) /???????/
-
- temp heapi1
/??????heap1?heapi1/ - heapi1 heap1
- heap1 temp
- adjust_heap(heap,1,i) /???? /
- printf("\n???? ")
- for ( j 1 j lt 10 j ) /??????/
27Heap Sort
- / ??? ??????????????. /
- int main()
-
- /???????/
- int data10 0, 5, 6, 4, 8, 2, 3, 7, 1, 9
- int i
- printf("?????? ")
- for ( i 1 i lt 10 i ) / ??????? /
- printf("d",datai)
- heap(data,9) / ????? /
- printf("\n\n?????? ")
- for ( i 1 i lt 10 i ) / ?????? /
- printf("d",datai)
- printf("\n")
- system("pause")
- return 0
28Heap Sort Result
29Merge Sort
- ??????????????.
- Step1?????????,??????????????,?????????????,?????
????????. - Step2?Step1?????,????????????,???????????????,???
??.
30Merge Sort
1
2
3
4
5
6
7
31Merge Sort
1 4 5 6
2 3 7 8
??1
??2
1 2 3 4
32Merge Sort
- void merge(FILE merge,FILE sort1,FILE
sort2,int len) -
- int s10,s20 / ???? /
- char c,c1,c2
- c1 getc(sort1) / ??????? /
- c2 getc(sort2) / ??????? /
- while ( 1 )
-
- if ( c1 lt c2 ) / ?????/
-
- / ??????, ???? /
- putc(c1,merge)
- s1
- if ( s1 lt len ) / ????? /
- c1 getc(sort1) / ?????????/
- else
- break / ????????,???? /
-
- / ???????????????? /
- if ( s1 lt len )
-
- putc(c1,merge) / ????/
- s1 / ????/
-
- / ???????????????? /
- if ( s2 lt len )
-
- putc(c2,merge) / ???? /
- s2
-
- / ????? /
- while ( s1 lt len ) / ??????? /
-
- c getc(sort1) / ??????? /
- putc(c,merge) / ???? /
- s1
-
33Merge Sort
- /????/
- include ltstdio.hgt
- define LEN 4 / ??????/
- / ??? ????????, ???????????. /
- int main()
-
- FILE fp / ???? /
- FILE fp1 / ????????/
- FILE fp2 / ???????? /
- fp fopen("result.txt","r") / ???? /
- if ( fp NULL )
- printf("??????! \n")
- else
- fp1 fopen("sort1.txt","r")
/????????/ - if ( fp1 NULL )
- printf("?????????! \n")
- else
-
????????sort1.txt,sort2.txt,result.txt