Lecture 7 Sorting in Linear Time - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Lecture 7 Sorting in Linear Time

Description:

Decision-Tree Model Decision-Tree Model ... for j = length[A] downto 1 do B[C[A[j]]] A[j] C[A[j]] C[A[j]]-1 } 7.3 Radix Sort ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 17
Provided by: mzhs
Category:
Tags: lecture | linear | radix | sorting | time | tree

less

Transcript and Presenter's Notes

Title: Lecture 7 Sorting in Linear Time


1
Lecture 7Sorting in Linear Time
2
7.1 Lower bounds for sorting
  • ??????????????????
  • ????????????????,??n????????O(nlogn)????
    ??????????O(nlogn)?
  • ????????? ??????,????????O(n)?????????

3
Decision-Tree Model
  • ???????????????????????????Decision-Tree?
  • ????Root?Leaf??????????????
  • ????????????n???????,????Decision-Tree?????O(nlogn
    )?

4
Decision-Tree Model
Eg. (a1 a2 a3) (9 2 6)
a1a2
gt
?
a1a3
a2a3
gt
?
?
gt
a1a3
a2a3
lt1,2,3gt
lt2,1,3gt
?
gt
?
gt
lt1,3,2gt
lt3,1,2gt
lt2,3,1gt
lt3,2,1gt
5
Decision-Tree Model
  • ???????n!????????,????Decision tree???n!?leaf
    nodes?????h???????2h?leaf nodes???h ? log2(n!) ?
    T(nlogn)?(???Stirlings approximation??n!gt(n/e)n)
  • Heapsort?Mergesort?asymptotically optimal???????

6
7.2 Counting Sort
  • Counting Sort (?????) ????????????
  • ??????????????????????(????????????,??1?k??)
  • ?????O(nk)
  • ???????,??1?k???????????,???????????????

7
  • Input A1..n, where Aj?1,2,,k
  • OutputB1..n, sorted
  • CountingSort(A,B,k)
  • for i 0 to k
  • do Ci?0
  • for j 1 to lengthA
  • do CAj?CAj1
  • for i 1 to k
  • do Ci?CiCi-1
  • for j lengthA downto 1
  • do BCAj?Aj
  • CAj?CAj-1

8
k6
1
2
3
4
5
6
7
8
A
3
6
4
1
3
4
1
4
1
2
3
4
5
6
C
2nd loop
2
0
2
3
0
1
1
2
3
4
5
6
2
2
4
7
7
8
3rd loop
9
4th loop
1
2
3
4
5
6
7
8
1
2
3
4
5
6
B
C
1st iteration
4
2
2
4
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
B
C
2nd iteration
4
1
6
2
4
1
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
B
C
3rd iteration
1
4
4
1
2
4
5
7
8

1
2
3
4
5
6
7
8
1
2
3
4
5
6
B
C
8th iteration
1
1
3
3
4
4
4
6
0
2
2
4
7
7
10
7.3 Radix Sort
  • Radix Sort(?????)?????????????
  • ??????????????????????(???????????,??d?)

11
Radix Sort
  • ?????????????????????

329 457 657 839 436 720 355
720 355 436 457 657 329 839
720 329 436 839 355 457 657
329 355 436 457 657 720 839
??????
?????
?????
12
  • Radix-Sort(A,d)
  • for i 1 to d
  • do use stable sort to sort A on digit i
  • ?????stable sort????Counting Sort???Iteration???T(
    n10)????
  • ??????O(d(n10))????
  • ??d???,?Radix Sort??????Linear time?????????

13
7.4 Bucket Sort
  • ?????????????,Bucket sort????O(n)????????
  • ?????n???A1..n????0,1??????
  • ??n???(bucket),B1..n,???x??x??????????????
    ??? ?

14
Bucket Sort
  • ???????,??Linked list???,???????????
  • ?????Lined list????,???????n????

15
A
B
.76
1
1
.07
.05
.07
2
2
.36
3
3
.29
.22
.25
.29
4
4
.36
.74
5
5
.95
6
6
.22
7
7
.66
.05
8
8
.76
.74
.25
9
9
.66
10
10
.95
???????????????List
16
???????
  • ?????i?????????ni?
  • ????T(n) O(n) O(n2).
  • ????T(n) O(n)
    O(n) O(n)
  • ????????
Write a Comment
User Comments (0)
About PowerShow.com