Title: Sorting
1?????????????????
Sorting
2????????
- ???????????????? (Sorting) ???????????????????????
? ??????????????????????????????????? (?????????
???? ?????????) - ?????????????????????? Record ?????? Field
??????????????????? Field ????????????????????????
?????? ???? ???????????????????????????????
???????????????????????????????????? Field
???????????????????? ???????
3????????????????????????????
- ???????????????? (Internal Sorting)
- ??????????????????????????????????????????????????
????????????????? ????????????????????????????????
???????????????????????????????????? ???? ??????
Array ???? Linked-List ?????????? - ????????????????? (External Sorting)
- ??????????????????????????????????????????????
???? Disk ??????????????????????????
???????????????????????????? ?????????????????????
????????????????? ????????????????????????????????
??????????????? ??????????????????????????????????
?????? ??????????????????????????????
4?????????????????????
- ???????????????????????? (Exchange Sort)
- ?????????????????? (Insertion Sort)
- ??????????????????? (Selection Sort)
5???????????????????????? (Bubble Sort)
- ??????????????????????????????????? 2
???????????? - ?????????????????????? ? ?????????????????????????
???????????? ???? ????????????????????????????????
? ????? - ?????????????????? ?????????????????????
- ????????????????? ?????????????????
- ?????? 2 ???????????????? ???
- ????????????????????????????? ????????????????????
?? - ???????????????????????????????????????
???????????????????? - ??????????????????????? ? ????????????????????????
????????????????????????????????????????????
6???????? Function ?????????????? Bubble
7???????? Bubble Sort
12
42
94
18
06
67
55
44
?????? 8 ????? 7 ???
?????? ?????? 1 06 44 55 12 42 94 18 67 2 06 1
2 44 55 18 42 94 67 3 06 12 18 44 55 42 67 94 4 06
12 18 42 44 55 67 94 5 06 12 18 42 44 55 67 94 6
06 12 18 42 44 55 67 94 7 06 12 18 42 44 55 67 94
8 int main() int AELEMENTS44,55,12,42,94,6,
18,67 int x coutltlt"NON SORTED
LIST"ltltendl for(x0xltELEMENTSx)
coutltltAxltltendl exchange_sort(A,ELEMENTS)
coutltltendlltlt"SORTED LIST"ltltendl
for(x1xltELEMENTSx)
coutltltAxltltendl return 0
include ltiostream.hgt define ELEMENTS 8 void
exchange_sort(int x,int length) int temp
for(int i0iltlengthi) for (int
jlengthjgtij--) if(xj-1gtxj)
tempxj-1 xj-1xj
xjtemp
9?????????????????? (Insertion Sort)
- ????????????????????????????????????????????????
? ????????????????? - ??????????????????????????????????????????????????
????????????????????????????????? ?
????????????????? - ??????????????????????????????????????????????????
??? ???? - ?????????????????????????
- ???????????????????? 2 ???????????????????????????
???????????????? - ??????????????? ????????????????? ? ??
??????????????????????????????????????????????????
????????? ????????????
10???????? Function ??????????? Insertion
11????????Insertion Sort
12
42
94
18
06
67
55
44
?????? 8 ????? 7 ???
?????? ?????? 2 44 55 12 42 94 18 06 67 3 1
2 44 55 42 94 18 06 67 4 12 42 44 55 94 18 06 67 5
12 42 44 55 94 18 06 67 6 12 18 42 44 55 94 06 67
7 06 12 18 42 44 55 94 67 8 06 12 18 42 44 55 67
94
12 int main() int AELEMENTS44,55,12,42,94,0
6,18,67 int x coutltlt"NON SORTED
LIST"ltltendl for(x0xltELEMENTSx)
coutltltAxltltendl insertion_sort(A,ELEMENTS
) coutltltendlltlt"SORTED LIST"ltltendl
for(x0xltELEMENTSx)
coutltltAxltltendl return 0
include ltiostream.hgt define ELEMENTS 8
void insertion_sort(int x,int length) int
key,i for(int j1jltlengthj)
keyxj ij-1 while(xigtkey
igt0) xi1xi
i-- xi1key
13??????????????????? (Selection Sort)
- ??????????????????????????????????????????????????
??????????????????????????????? - ???????????????????????
- ??????????????????????????????????????????????????
????????
14???????? Function ??????????? Selection
15????????Selection Sort
12
42
94
18
06
67
55
44
?????? 8 ????? 7 ???
?????? ?????? 1 06 55 12 42 94 18 44 67 2 06 1
2 55 42 94 18 44 67 3 06 12 18 42 94 55 44 67 4 06
12 18 42 94 55 44 67 5 06 12 18 42 44 55 94 67 6
06 12 18 42 44 55 94 67 7 06 12 18 42 44 55 67 94
16 int main() int AELEMENTS44,55,12,42,94,6
,18,67 int x coutltlt"NON SORTED
LIST"ltltendl for(x0xltELEMENTSx)
coutltltAxltltendl selection_sort(A,ELEMENTS
) coutltltendlltlt"SORTED LIST"ltltendl
for(x0xlt ELEMENTSx)
coutltltAxltltendl return 0
include ltiostream.hgt define ELEMENTS 8 void
selection_sort(int x,int length) int
k,temp for(int i0iltlengthi)
ki for (int ji1jlt lengthj)
if(xjlt xk) kj
tempxi xixk xktemp
17LAB 1
????????????????????????????????
?????????????????????????? (Sorting) ???? 3 ???
???????????????????????????? Output ???? 3
??????????????
18 Insertion Sort ???????????????
12
42
94
18
06
67
55
44
?????? 8 ????? 7 ???
Insert2.cpp
?????? ?????? 2 44 55 12 42 94 18 06 67 3 4
4 55 12 42 94 06 18 67 4 44 55 12 42 06 18 67 94 5
44 55 12 06 18 42 67 94 6 44 55 06 12 18 42 67 94
7 44 06 12 18 42 55 67 94 8 06 12 18 42 44 55 67
94
19Bubble Sort ??????????????????????
12
42
94
18
06
67
55
44
?????? 8 ????? 7 ???
Bubble2.cpp
?????? ?????? 1 44 12 42 55 18 06 67 94 2 12 4
2 44 18 06 55 67 94 3 12 42 18 06 44 55 67 94 4 12
18 06 42 44 55 67 94 5 12 06 18 42 44 55 67 94 6
06 12 18 42 44 55 67 94 7 06 12 18 42 44 55 67 94
20Selection Sort ???????????????????????????????
12
42
94
18
06
67
55
44
?????? 8 ????? 7 ???
Select2.cpp
?????? ?????? 1 44 55 12 42 67 18 06 94 2 44 5
5 12 42 06 18 67 94 3 44 18 12 42 06 55 67 94 4 06
18 12 42 44 55 67 94 5 06 18 12 42 44 55 67 94 6
06 12 18 42 44 55 67 94 7 06 12 18 42 44 55 67 94