sorts - PowerPoint PPT Presentation

About This Presentation
Title:

sorts

Description:

Data Structures: Sorts, CS, TAU. 1. ?????? (sorts) ?????? ???????? ???? ... Data Structures: Sorts, CS, TAU. 13. ????? ? HEAPSORT. 1) For X on list L do. 2) ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 39
Provided by: CHA168
Category:
Tags: cs | sorts

less

Transcript and Presenter's Notes

Title: sorts


1
?????? (sorts)
2) ???? ????? Insertion Sort - ??????? ? i
?????? ????????? A1,.,Ai ?????? ????
???? ???? (?? ??? ??????)
3) Selection Sort - ???? ? i ?????? ?? ????
????? ??? Ai,,An ????? ????? ? i.
2
2) Insertion- ?? ????? ?????? ??? ???? ? Bubble
3
3) Selection Sort- - ??? ?????? ???????
?????? ??? ?????? ??????. - ???? ?????? (O(N
????? - ?????????? ?????? (W(n2), O(n2 -
?????????? ?????? (O(nlogn - ????? ?????
?????? ????? ????? ??????- ???? - ?????
4
QUICKSORT
??????? ????? (O(nlogn ???? ????? (O(n2 ????
?? ??????? Aj,., Ai ?????
???? 1) ??? ?????, V (??? ????? Aj,..,Ai)
2) ??? (?????) ????? k ?? ? ?????? ??????
?- V ??????? ?- Aj,..,Ak ???????? ?????
?- V ??????? ?- Ak1,..,Ai
3) ???? ?? Aj,..,Ak ?- Ak1,..,Ai
?????????
????? ?????? ????? ????? ????? ??? ?????? ??????
?? ??? ?????
5
????????? ????? ? 1) ????? ????? FindPivot 2)
????? Partition 3) ???????
1) FindPivot ?? ????? ????? ???? ?? ????? ??????
???? ??????
6
????? ???????? (Partition(i, j ??? (O(j - i 1
?) QuickSort- ??? ?????? ??????????, ???
?????? ????? ???????? ??? ???? ??? ?? ??
???????? ?? ???? ?????? ???????? ??????
???? ??????? ?? ???????? (????)
7
?) Worst Case ?? ?? ??? ???? ?????? ?? ?????
????? ?? ????? ????? n ????? ????? ?? 1 ???? ??
n -1
8
?) ????? ???? 1 ?? ???????? ????? ?? ???
(??????? ???? ?? ??????? ????????)
???? 2 ?? ?????? ?? Ai,,Aj ???? ???????!
?????!? ??? ?? ???? ?????? ??? ???????? ?? ????
??? ?????? ????? ?????? ?????
??? ?????? ??????- ?????? ????? ??? ??? ????
?? ????? ?????
??? ??? - ?? ?? ?? ??????? (???? ????) - ??
?? ??????? ???? ??????? ????? ??????
9
????? (T(n ??? ???? ???? ????? n.
10
(No Transcript)
11
  • ????? ????????
  • QuickSort- ????? ????? ?????? ??? ????????? ?
    nlogn
  • ???? ?????????
  • ???? ????? ??????? ???? ????? ????.
  • ????? ?????? ???? ????? ???????
  • ????? ?? k ??????? ???? ?????
  • TradeOff
  • k ??? gt ???? ?????
  • k ???? gt ????? ????? ????
  • ????? ?? ???? ?????????
  • - ????? ?????? ? n 2
  • - ???? n ??? ?? ???? ??? ?????????? ???? (O(n2
  • - Knuth ????? ?? n 9
  • ????? ????????? ???????
  • - ???? ??????
  • - ???? ????????

12
HEAPSORT
?????? ? HEAP ?? ???? ??? ????? HEAP ??? ?????
?? ?? ????? ???? ????? ????.
?????? INSERT DELETEMIN
13
????? ? HEAPSORT
1) For X on list L do 2) INSERT(X,heap)
3) While not EMPTY(heap) do 4)
yDELETEMIN(heap) 5) Print(y) 6) end
?? ?? ????? ?????- ???? ????? ?????
????????? ???? ? A1,,Ai ??? Ai ????? gt
???? ?? ?? ?????? gt ???? ???? ?????? ???? ????
???????? ??? For i n to 2 do
SWAP(Ai,A1) PUSHDOWN(A1)
14
(No Transcript)
15
??? n/2 ?????? gt ??? ????!
?? ??? ???? ????? HEAP-? ????? ?????
16
?????? ?????? ?? ???? ???? ??????? ???? ????
???? ????? HEAP-?. ??? ??? ?????? ????? ???????
??? (Pushdown(i ????? ??? ??? ??? ???? ?-i ????
???????
?????? 1) ???? HEAP n/2 ????????- ?? ??? ?????
logn gt (O(nlogn
2) ????? ? sort n ??????- ?? logn gt
(O(logn
17
????? 1) O(nlogn) HEAP ? worstcase
2) ????? ??? ???? ???? ? QUICKSORT
18
MERGESORT
  • ???? ?? ??? ????? ??????
  • ?? ????? ??????
  • gt (O(nlogn

(Cormen 13-15)
19
BIN SORTING
?????? ?????? ?? ?? (O(nlogn ??? ???? ???? ?????
? (O(nlogn?
????? (W(nlogn ?? ?? ?????? ???? ?? ???????
???? ?? ?? ??????- ???? ????
????? 1 ?? ?????? ?????? A1,,n ?????? ???????
n,.,1 ??? ???? ???? B ? (O(n BAi.key Ai
20
????? 3 ?????? ?? ????? 1, ???? ???? A ??
Ai.key j ???? Ai ?? Aj
From i 1 to n do while Ai.key ltgt i do
swap(Ai, AAi.key)
?????? (O(n ????? (O(n ?????? (???? ???? ??????
?? ????? ????!)
BIN SORTING ???? ???? ??????? ???? ???? (BINS)
??????- ????? ????? - ????? 1 ??? BIN-SORT
???? ???? BIN ???? (1) ????? ????? ???? ?????
?????? ????? ?) ???? ???? ???? BIN ?) ??? ???
BIN-??
21
????? 1) ?? BIN ????? ?????? 2) HEADERS
??????? ?? ????? ??????
????? (O(1 ????? (O(1
??? ???? ????? ???? ??????? ?? ?????? ???? n ????
22
?????? m - ???? ?????? ???????? (???? ?????) n -
???? ???????
????? ???? ?? ??????? i2 ???? i1,2,..,10 ?????
???? ?? 100,....,0,1,4
23
????? - ??? n ???? - ???? ??? ????? ?????
???????? - ???? ??? ????? ????? ????????
24
??? ????? ???? i 10a b, j 10c d
???? i lt j
- ?? a c ??? b lt d ???? ???? ????? ?????
???? ??? i ???? ??? ???? j ???? ????
??? ??? BIN SORT? - ?????? ?????? ?????
?????????? ??? 1,,nk (???? k) - ??????
????? k
25
??? ???? ???? ?? ?? k ???? ????!! ????? n 100,
k 100 nk BIN SORT (100 ??????? ??
100 ??????) ???? ??? nlogn ?- nk gt
nlogn
??? ?????? ????????! ????? ????- ??????
(O(k ???? ?? ?????? ????? ???????!!!
26
RADIX SORT
???? ? BIN SORT ?????, ?? ???? ??? ??? ?????? ??
???? ????? ???.
27
??? ????? ????? ???????? - ?????? ???????
(W(nlogn - ?????? ?????? (W(n
???? ??????? ?????? ??? ??????? (??/??)
????? ?????? ?? ???? ????????.
??? ???? ?????? ????? ??? ????? ?? ????? ??????
???? ??????? (????? ??????)
????? ???? ?????? ?? ????? - ?? ???? ????? ??
???? ????????? - ?????? ?????? ????? ?????
??????, ??? ??????.
28
(No Transcript)
29
??? ????? ???? ???? ??????? ????? ??, ???? ???
???? ????? ??? ???? ??????.
- ?? ???? ?????? ????? ??.
- ???? ?? ?????? ???.
- ?? ??? ?????? ?? ??? k ????, ??? ???? ?????
?????? ????? logk!! (?? ?????? ??? ?????? gt
?????? logk, ?? ?? ??? gt ???? ????!!)
- ????? ??????? ????? ??????? ??? ????!
30
??? ????? ???? ????? - ???? ?????? ??? ???
(W(nlogn - ???? ?????? ?????? ??? ?????? k ????
- ???? ????? ????? logk.
????? ?????? ??? T ???? ????? ?? ??? ??????.
?? ? T ?? ??? ?? ?????
31
- ????? ??? ????? ??????, ?? ????? ??????? ???
(??? ????? k1k2k)
32
????????? ??? ?????? ????? Order - Statistics and
Median
ith order statistic ????? ? i ??????.
???? ?????? n ??????, ??? ?? ????? ? i ??????.
????? 1) ??? ??????? 2) ??? ????? (Median) -
????? ?????? ??????.
1) ????? ??????? max ? A1 for i ? 2 to
length(A) do if max lt Ai then max ?
Ai return max
???????? (O(n
33
2) ????? ??????? ??? (O(n
34
?????? ?? ????? ??? ????????? (O(n
?) ????? ?????? ???? ????? (O(n ??? partition
????? ??? ??? ????? 1) ???????? O(n2) worst
case ?? ??????? 1 n ?????? 2 n-1 ??????
??? 2) ???????? ??????
35
(?? ???? ????? c ???? ?????)
36
?) ????? statistic ?- O(n) worst case ??? ?????
i ???? n ?????? (select) 1) ??? ??????? ? n/5
?????? ?? 5 ?????? 2) ??? ?? ?????? ?? ??
???????? (???? ????) 3) ????? ????????? ? select
?????? ?????? ?? ???????? 4) ??? partition ?????
?????? ???????? ??? k ???? ??????? ???? ????? ???
n-k ???? ??????? ???? ????? 5) ????? ? select
????????? ?????? i ???? ????? ?? i-k ???? ?????
37
???? ?????? ??? ? 1 ?(O(n
38
(???? ????? c ???? ????? ?? ? (c(n/10-7 ????
????????? ??????? ?? (O(n
Write a Comment
User Comments (0)
About PowerShow.com