????? 9 ??????????????????? (Sorting) - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

????? 9 ??????????????????? (Sorting)

Description:

Title: (Office Automation) Author: Nittaya Last modified by: MooKy Created Date: 12/28/2005 2:54:17 AM – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 53
Provided by: Nitt4
Category:

less

Transcript and Presenter's Notes

Title: ????? 9 ??????????????????? (Sorting)


1
????? 9 ??????????????????? (Sorting)
  • By Juthawut Chantharamalee

2
????????????????????????? (Sort Classifications)
??????????????????????????? (Internal Sorting)
  • ???????????????????????????????????????????
    (Primary Memory) ?????????????????????
    ??????????????????????????????????????????????????
    ???????????????????????????? ?????????????????????
    ????????????????????????????????????????????
    ????????????????????????????????????????????
    ?????????????????????????????????????????? ????
    Insertion Sort, Shell Sort, Selection Sort, Heap
    Sort, Bubble Sort, ??? Quick Sort ???????

3
???????????????????????????? (External Sorting)
  • ???????????????????????????????????????????
    ?????????????????????????????????????????????
    ???????? ?????????????????????????????????????????
    ??????????????????????????????????????????????????
    ??????????????????????? ??????????????????????????
    ?????????????????????????????????????????????????
    ?????????????? (Secondary Storage) ????
    ?????????? ??????? ???????????????????????????????
    ???????????????????????????????????????????

4
Selection Heap Insertion Heap
Bubble Quick
?????? 9.1 ????????????????????????????
5
1. ??????????????????????????? (Internal)
1.1 ????????????????????????? (Selection)
  • 1.11 ?????????????????????? Selection Sort
    1.12 ?????????????????????? Heap Sort

1.2 ???????????????????????? (Insertion)
1.21 ?????????????????????? Insertion Sort
1.22 ?????????????????????? Shell Sort
6
1.3 ?????????????????????????????? (Exchange)
  • 1.31 ?????????????????????? Bubble Sort
    1.32 ?????????????????????? Quick Sort

2. ???????????????????????????? (External)
2.1 ????????????????????????
2.21 ?????????????????????? Merge Sort
7
???????????????? (Sort Order)
  • ??????????????????????????????????????????
    (Ascending) ????????????????????? (Descending)
    ????? ????????????????????????????????????????????
    ????????????????????? ????????????????????????????
    ???????????????????? ???? ???????????? ?????????
    ??????????????????????????????? ????
    ????????????????????????? ????????????????????????
    ???????????????????????????????????? ???????

8
????????????????????????????????? (Sort
Efficiency)
  • ????????????????????????????????? ???
    ??????????????????????????????????????????????????
    ??? ??????????????? ??????????????????????????????
    ???? ?????????????????????????????????????????????
    ???????????????????????????? ?????????????????????
    ??????????????????????????????????????????????????
    ????? ????????????????????????????????????????????
    ?????????????????????????????????? ????
    ????????????????????????????? O(n) ??????????
    O(n2)

9
???????????????????????
?????????????????????? Selection Sort
  • ?????????????????????? Selection Sort
    ???????????????????????????????
    ?????????????????????????????????????????????
    ??????????????????? ????????????????????????????
    ??????????????????????????????????????????????????
    ??? ??????????????????????????????????????????????
    ?????????????????????? ?????????????????
    ???????????????????????????? ?????????????????????
    ?? ??????????? ???????????????????????????????????
    ????????

10
Wall

Minimum ( ak alast)
0 j k
Last
Sorted Unsorted
?????? 9.2 ?????????????????????????? Selection
Sort
11
Ex. ????????????????? Selection Sort
??????????????????????????? 9.2
????????????????????????????????????????????
23, 78, 45, 8, 32, 56 ?????????? 1
???????????????????????? ????????????????????????
???????????????????????? ?????????????????????????
????????????????? 1 ?????????????? 6 ???????????
8 ????????????????????????????????? 23
?????????????????????????????????????????????????
23
78
45
8
32
56
8
78
45
23
32
56
Unsorted
Unsorted
12
?????????? 2 ?????????????????? 2
????????? 78 ?????????????? 6 ??????????????????
23 ????????????????????????????????????????? 78
??? 23 ???????????????????????????????????????????
??????
8
23
45
78
32
56
8
78
45
23
32
56
Unsorted
Sorted Unsorted
13
?????????? 3 ?????????????????? 3
????????? 45 ?????????????? 6 ??????????????????
32 ????????????????????????????????????????? 45
??? 32 ???????????????????????????????????????????
??????
8
23
32
78
45
56
8
23
45
78
32
56
Sorted Unsorted
Sorted Unsorted
14
?????????? 5 ????????????
????????????????? ????????????????????????????????
??????????????????? 78 ??? 56 ??????????? 56
???????? ?????????????????????? ???????????
??????????????????????????????????????????????????
?????
8
23
32
45
56
78
8
23
32
45
78
56
Sorted
Sorted Unsorted
15
Algorithm seleetionSort (list, last) Sorts list
array by selecting smallest element in unsorted
portion of array and exchanging it with
element at the beginning of the unsorted list
Pre list must contain at least one
item last contains index
to last element in the list Post list
has been rearranged smallest to largest 1 set
current to 0 2 loop (until last element
sorted) 1 set smallest to current 2
set walker to current 1 3 loop (walker
lt last) 1 if (walker key lt smallest
key) 1 set smallest to walker
2 increment walker 4 end loop
Smallest selected exchange with current
element. 5 exchange (current, smallest)
6 increment current 3 end loop end
seleetionSort
????????????? 9.1 Selection Sort
16
?????????????????????? Heap Sort
  • ?????????????????????? Heap Sort
    ??????????????????????????????????????????????????
    ????????? ????????????????????????????????
    ??????????????????????? ????????????????????????
    ??????????????????????????????????????????????????
    ??????? ??????????????????????????????????????????
    ??????????????????????????????????????????????????
    ???????????????

17
78
0
32
56
1
2
45
8
23
19
78
56
32
45
8
23
19
3
4
5
6
0 1 2 3 4 5 6
(b) ??????????????????????
(a) ??????
?????? 9.3 ?????????????????????????????????????
18
78
32
56
8
23
45
After heap
heap
After pass 1 and reheap
56
32
45
8
23
78
heap
sort
After pass 2 and reheap
45
32
23
8
56
78
heap
sort
After pass 3 and reheap
32
8
23
45
56
78
heap
sort
After pass 4 and reheap
23
8
32
45
56
78
heap
sort
After pass 5 and reheap
8
23
32
45
56
78
heap
sort
After pass 6 and reheap
8
23
32
45
56
78
sort
?????? 9.4 ????????????????????????????? Heap Sort
19
Algorithm heapSort (heap, last) Sort an array,
using a heap. Pre heap array is
filled last is index to last
element in array Post heap array has
been sorted Create heap 1 set walker
to 1 2 loop (heap built) 1 reheapUp
(heap, walker) 2 increment walker 3
end loop Heap created. Now sort it. 4
set sorted to last 5 loop until all data
sorted) 1 exchange (heap, 0, sorted)
2 decrement sorted 3 reheapDown (heap,
0, sorted) 6 end loop end heapSort
????????????? 9.2 Heap Sort
20
??????????????????????????????????????????????
Heap ???????????????????????????????????????
O(nlogn))
Number of loops
n
Selection Sorted
Heap Sorted
25
625
116
100
10,000
664
500
250,000
4,482
1000
1,000,000
9,965
2000
4,000,000
10,965
?????? 9.5 ???????????????????????????????????
Selection Sort ??? Heap Sort
21
?????????????????????? Insertion Sort
  • ?????????????????????? insertion Sort
    ?????????????????????????????? ???????????????????
    ???????????????????????????????
    ??????????????????????????????????????????????????
    ?????? ???????????????????????????????????????????
    ??????????????????????????????????????????????????
    ???????????? ??????????????????? 9.6
    ?????????????????????????????????????????
    Insertion Sort

22
Wall


0 j k
Last
Sorted Unsorted
?????? 9.6 ?????????????????????????? Insertion
Sort
23
Ex. ????????????????? Insertion Sort
??????????????????????????? 9.6
????????????????????????????????????????????
23, 78, 45, 8, 32, 56 ?????????? 1
???????????????????????? ????????????????????????
????????????????? ????????????????????????????????
??????????? 2 ??????????????????????????????????1
?????????? 2 ????????????????? 2 ???????????? 78
???????????????? 23 ???????????????????????????
????????????????????????????????????????????????
23
78
45
8
32
56
23
78
45
8
32
56
Sort Unsorted
Unsorted
24
?????????? 2 ????????????? ? ???????????????????
???????? 3????????????????????????????? 3 ??
???????????????????????????????????????????????
???????????????? ????????????????????????? 45
?????????????????????? 2 ?????????????????? 23
??? 78
23
78
45
8
32
56
23
45
78
8
32
56
Sort Unsorted
Sort Unsorted
25
?????????? 3 ????????????? ? ???????????????????
???????? 4 ????????????????????????????? 4 ??
??????????????????????????????????????????????????
???? ??? 8 ???????????????????????????????????????
???????????????
23
45
78
8
32
56
8
23
45
78
32
56
Sort Unsorted
Sort Unsorted
26
?????????? 4 ?????????????????????? 5
???????????????????????? 5 ?????????????????? 32
?????????????????????????????????????????????????
????????????? 32 ?????????????????? 5
??????????????????????????????????????????????????
???????????????? 45
8
23
45
78
32
56
8
23
32
45
78
56
Sort
Sort Unsorted
27
?????????? 5 ???????????????????????
??????????????????????????????????????????????????
???????????????????????????????????????
?????????? ????????? 56 ??????????????????????????
?? 5 ?????????????????????????????????????????????
??????????????
8
23
32
45
78
56
8
23
32
45
56
78
Sort
Sort
28
Algorithm insertionSort (list, last) Sorts list
array using insertion sort. The array is divided
into sorted and unsortsd lists. With each pass,
the first element in the unsorted list is
inserted into the sorted list. Pre list
must contain at least one item
last is an index to last element in the list
Post list has been rearranged 1 set current
to 1 2 loop (until last element sorted)
1 move current element to hold 2 set walker
to current - 1 3 loop (walker gt 0 AND hold
key lt walker key) 1 move walker element
right one element 2 decrement walker
4 end loop 5 move hold to walker 1
element 6 increment current 3 end
loop end insertionSort
????????????? 9.3 Insertion Sort
29
?????????????????????? Bubble Sort
  • ?????????????????????? Bubble Sort
    ??????????????????????????????????????????????????
    ???????????????????????????????????
    ????????????????????????????????????????
    ????????????????????????????????????????????
    ????????????????????????????????????????????
    ??????????????????????????????????????????????????
    ???????? ????????????????????????????????????
    ???????????????????? (Swap) ??????????????????????
    ?????????????????????? ???????????????????????????
    ????????????

30
Bubble up
Wall


0 j k
Last
Sorted Unsorted
?????? 9.7 ?????????????????????????? Bubble Sort
31
Ex. ????????????????? Bubble Sort
??????????????????????????? 9.7
????????????????????????????????????????????
23, 78, 45, 8, 56, 32 ?????? 1
23
78
45
8
56
32
23
78
45
8
32
56
23
78
45
8
32
56
23
78
8
45
32
56
23
8
78
45
32
56
8
23
78
45
32
56
Unsorted
32
?????? 2
8
23
78
45
32
56
8
23
78
45
32
56
8
23
78
32
45
56
8
23
32
78
32
56
8
23
32
45
32
56
Sorted Unsorted
33
?????? 3
?????? 4
8
23
32
78
45
56
8
23
32
45
78
56
8
23
32
45
78
56
8
23
32
45
56
78
8
23
32
45
78
56
8
23
32
45
56
78
8
23
32
78
32
56
8
23
32
45
56
78
Sorted Unsorted
Sorted
34
Algorithm bubbleSort (list, 'last) Sort an
array using .bubble sort. Adjacent elements, are
compared gad exchanged until list is completely
ordered. pre list mast contain at
least one item last contains
index to last element in the list Post
list has been rearranged in sequence low to
high 1 set current to 0 2 set sorted to
false 3 loop (current lt last AMD sorted
false) Each iteration is one sort pass.
1 set walker to last 2 set sorted to
true 3 loop (walker gt current) 1
if (walker data lt walker - 1 data)
Any exchange means list is not sorted.
1 set sorted to false 2 exchange-
(list, walker, walker - 1 2 end if
3 decrement walker 4 end loop
5 increment current ' 4 end loop end
bubbleSort
????????????? 9.4 Bubble Sort
35
?????????????????????? Quick Sort
  • ?????????????????????? Quick Sort
    ????????????????????????????????????????????????
    (Partition) ?????????????? ???????????????????????
    ???????????????????????????????????? Pivot
    ????????????????????????????????????
    ?????????????????????????????
    ??????????????????????????? ??????????????????????
    ?? ????????????? 3 ??????????? ???????????????????
    ????? ????????????????????????????????????????????
    ??????????? Bubble Sort

36
After 1st partition
1. ???????????? ??????????????????????????????????
??????????? pivot key 2. ??????????
?????? pivot key ????????????????????????????????
??????????????????????????????????????????????????
?????????????????????? pivot key
????????????????????????????????????? pivot key
3. ?????????? ??????????????????????
??????????????????????????????????????? pivot key
keys lt pivot pivot
keys gt pivot
After 2nd partition
lt pivot pivot gt pivot
After 3rd partition
lt--- Sorted --gt
After 4th partition
lt-------- Sorted --------gt
After 5th partition
lt-------- Sorted --------gt lt pivot pivot
gt pivot
After 6th partition
lt------------- Sorted ----------------gt

After 7th partition
lt------------------- Sorted ------------------gt

?????? 9.8 ???????????????????????????????????????
???? Quick Sort
37
  • ?????????????????????????????????????????????
    ?? Quick Sort ??????????? Hoare ????
    ????????????? Pivot Key ??????????
    ???????????????????? ???????????? ?.?. 1969, R.C.
    Singleton ???????????????????????????????? Pivot
    Key ?????????? (median value) ?????????????? 3
    ???????????????? ???????????????????? (Left)
    ???????????????? (Right) ?????????????????????????
    ????????? ????????????????????????????????????????
    ?????????????

Left element lt middle element lt right element
38
Algorithm medianLeft sortData, left, right) Find
the median value of an array and place it in the
first(left) location. Pre sortData is
an array of at least three elements
left and right are the boundaries of the
array Post median value locate and place
left Rearrange sortData so median value is
in middle location. 1 set mid to (left
right) /2 2 if (left key gt mid key) 1
exchange (sortData, left, mid) 3 end if 4
if (left key gt right key) l exchange
(sortData, left, right) 5 end if 6 if
mid key gt right key) 1 exchange
(sortData, mid, right) 7 end if Median
Is in middle location. Exchange with left. 8
exchange (sortData, left, mid) end medianLeft
????????????? 9.5 Median left
39
Ex. ????????????????? Quick Sort
?????????????????????????????????????????????????
78, 21, 14, 97, 87, 62, 74, 85, 76, 45, 84,
22 ??????????????????????????????????????
set mid to (left right) / 2 ?????????????? 1
????????????? 9.5 ???????????? (1 12)/2
??????????????? 6 ?????????????? Left Key 78,
Mid Key 62 ??? Right Key 22
??????????????????????????????????????????????????
????? ????????????????????????????????????????????
??????? 9.5 ??????????????????????????????????????
??????????? ??????????????? Left Key, Mid
Key ??? Right Key
L
M
R
78
21
14
97
87
62
74
85
76
45
84
22
?????????????? Left Key gt Mid Key ???????
????????????????????
62
21
14
97
87
78
74
85
76
45
84
22
40
?????????????? Left Key gt Right Key ???????
????????????????????
22
21
14
97
87
78
74
85
76
45
84
62
?????????????? Mid Key gt Right Key ???????
????????????????????
22
21
14
97
87
62
74
85
76
45
84
78
? ???????????????????????????????
Left element lt middle element lt right element
????????? 22 lt 62 lt 78 ?????????????????????????
???? Pivot Key ??????????????????????????????????
???????????????????????????????????????
??????????????????????????????????????????????????

41
Pivot Key
62
21
14
97
87
22
74
85
76
45
84
78
sortLeft
sortRight
??????????????????????????????????? sortLeft
??? sortRight ????????????????????????????????????
???? ?????????? sortLeft ???????????????? Pivot
Key ?????????????? ???????????????????????????????
????????? ??????????????????? sortLeft ???
sortRight gt sortLeft ???????????????????????
????? sortLeft ????????????? Pivot Key gt
sortRight ???????????????????????? ?????
sortRight ???????????? Pivot Key
???????????????????????? gt ?????? sortLeft
???????????? Pivot Key gt ?????? sortRight
????????????? Pivot Key
42
????????????????????????????????????????????????
??????????????
Exchange
62
21
14
97
87
22
74
85
76
45
84
78
sortLeft
sortRight
?????????????????????????????????????????
??????????????????????????? ??????????????????????
????????????????
Exchange
62
21
14
45
87
22
74
85
76
97
84
78
sortLeft
sortRight
?????????????? sortLeft ????????????????????????
????????? sortRight ???? ?????????????????????????
???? ?????????????????????????????????????????????
?????????? Pivot Key ????????? 22
????????????????????? ????????????????? Pivot Key
??????????????????????????
43
Move
62
21
14
45
22
87
74
85
76
97
84
78
sortRight
sortLeft
????????????????????????????????????????????????
????????? Pivot ??????????????????????????????
Pivot ????????????????????????????????????????????
?? Pivot ????????????????????????????????????
22
21
14
45
62
87
74
85
76
97
84
78
lt pivot pivot
gt pivot
?????????????????????? Pivot Key
???????????????????????????? ?????????????????????
?????????????????????? (Recursive)
?????????????????????????? Quick Sort
??????????????????????????????????????????????????
????????????????????? ????????? 9.9
44
78
21
14
97
87
62
74
85
76
45
84
22
62
22
21
14
45
87
74
85
76
97
84
78
22
78
14
21
45
76
74
87
97
84
87
45
14
87
74
21
76
84
85
97
21
76
97
84
85
?????? 9.9 ???????????????????????????????????????
???? Quick Sort ????????????? Recursive
85
45
Algorithm quicksort (list, left, right) An array,
list, is sorted using recursion. Pre
list is an array of data to be sorted
left and right identify the first and
last elements of the list,
respectively Post list is sorted 1
if (right - left) gt minSize) Quick
sort 1 raedianLeft (list, left, right)
2 set pivot to left element 3 set sortLeft
to left l 4 set sortRight to right
5 loop (sortLeft lt sortRight) Find
key on left that belongs on right 1 loop
sortLeft key lt pivot key) l
decrement sortRight 2 end loop
Find key on right that belongs on left
3 loop (sortRight key gt pivot key) 1
decrement sortRight 4 end loop
5 if (sortLeft lt sortRight) 1
exchange(list, sortLeft, sortRight) 2
increment sortLeft 3 decrement
sortRight 6 end if 6 end loop
Prepare for next pass 7 move sortLeft -
1 element to left element 8 move pivot
element to sortLeft - 1 element 9 if (left
lt sortRight) 1 quicksort (list, left,
sortRight -1) 10 end if 11 if
(sortLeft lt right) 1 quicksort (list,
sortLeft, right) 12 end if 2 else
1 insertionSort (list, left, right) 3 end
if end quicksort
????????????? 9.6 Quick Sort
46
?????????????????????? Merge Sort
  • ?????????????????????? Merge Sort
    ????????????????????????????????
    ?????????????????????????????? ???????????????????
    ???????????????????? ?????????????????????????
    ??????????????????????????????????????????????????
    ?????????????????????????????????????????????????
    ??????????????????? ??????????????????????????????
    ??????????????????????????????? ????????? 9.10
    ??????????????????????????????????????????????????
    ?? Merge Sort ??????????????????????????????????
    ????????? 2 ??????????????????????????????????????
    ?????????

47
Input file
2,300 recodes
Sort
Merge 1
Merge 2
1-500
1001-1500
2001-2300
501-1000
1501-2000
?????? 9.10 ??????????????????????????????????????
???????????????? Merge Sort
48
????????????????????????????????????????????
Merge Sort ???? ??????????????????????????????????
??????????????????????????????????????????????????
???????? ???????? ???????????????????? File-1 ???
File-2 ????????????????? ?????????????????????????
File-3 ????????????? 9.10
File-1
File-2
1
3
5
4
5
6
8
10
File-3
1
2
3
4
5
6
8
10
?????? 9.10 ??????????????????????????????????????
??????????????
49
Algorithm mergeFiles Merge two sorted files into
one file, Pre input files are sorted
Post input files sequentially combined in
output file 1 open files 2 read (file1
into recordl) 3 read (file2 into record2) 4
loop (not end filel OR not end file2) 1
if (recordl key lt record2 key) 1 write
(recordl to file3) 2 read (file into
recordl 3 if (end of file1)
1 set recordl key to infinity 4
end if 2 else 1 write (record2
to file3) 2 read (file2 into
record2) 3 if (end of file2)
1 set record2 key to infinity 4
end if 3 end if 5 end loop 6 close
files end mergeFiles
????????????? 9.7 Merge Sort
50
Ex. ????????????????? Merge Sort
?????????????????????????????????????????????????
1, 5, 3, 14, 17, 6, 7, 13
1
5
3
14
17
6
7
13
Distribution Phase
1
5
3
14
17
6
7
13
1
5
3
14
17
6
7
13
1
5
3
14
17
6
7
13
1
5
3
14
6
17
7
13
1
3
5
14
6
7
13
17
1
3
5
6
7
13
14
17
?????? 9.11 ?????????????????????????? Merge Sort
51
?????????????? 9.12 ????????????????????????????
?????????????????????????? ??????????????????
?????????? ?????????????????
???????????????????????
???????????? (Best-case)
?????????? (Average-case)
????????????????? (Worst-case)
Selection Sort
O(n2)
O(n2)
O(n2)
Insertion Sort
O(n)
O(n2)
O(n2)
Bubble Sort
O(n)
O(n2)
O(n2)
Quick Sort
O(nlogn)
O(nlogn)
O(n2)
Merge Sort
O(nlogn)
O(nlogn)
O(nlogn)
?????? 9.12 ??????????????????????????????????????
????????????????????
52
  • The End
  • Lesson 9
Write a Comment
User Comments (0)
About PowerShow.com