(Heap Sort) - PowerPoint PPT Presentation

About This Presentation
Title:

(Heap Sort)

Description:

(Heap Sort) (0 ) ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 30
Provided by: Wen114
Category:
Tags: heap | sort | swap

less

Transcript and Presenter's Notes

Title: (Heap Sort)


1
?????
  • (Heap Sort)

2
  • ????????,?????????(0?)?????????????????
  • ?????????,????????????????????????,
  • ???????,?????????????????????,??????????
  • ????????,????????(Heap Sort)??

3
??
  • ???????????????????,??John Williams???
  • ????????,???????????,????logn??,??????????????????
    ?O(nlogn),??????????????,??????????????
  • ????????????????????(heap tree)

4
??????
  • ?????????
  • ?????????????????????

??a ???12,55,618,40,60,47,25,1,30,6,66?????????
???
5
?????  
6
  • ????????,??????
  • ???i??????????N?????
  • i N / 2 -1
  • ??j???i??????????
  • if ( a 2i 1 gt a 2 i 2 )
  • j 2 i 1
  • else
  • j 2 i 2
  • ????i???????j????,???i?j??,???i ???j?
  • if ( a i gt a j )
  • swap ( a i , a j )
  • i j
  • ????i????????step 2
  • ??i??0,?i i 1,??step 2

7
(a) 60?66??
8
(b)55?66???,??60??
9
(c) 12?618???,??47??
10
????
  • ????????????

11
(a) 618?55??
a 0 1 2 3 4 5 6 7 8 9 10
? 55 66 47 40 60 12 25 1 30 6 618
???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ????
12
(b)??55
a 0 1 2 3 4 5 6 7 8 9
? 66 60 47 40 55 12 25 1 30 6
13
(c) 66?6??,??6
a 0 1 2 3 4 5 6 7 8 9
? 60 55 47 40 6 12 25 1 30 66
???? ???? ???? ???? ???? ???? ???? ???? ???? ???? ????
14
(d)66?30??,??30
a 0 1 2 3 4 5 6 7 8
? 55 40 47 30 6 12 25 1 60
???? ???? ???? ???? ???? ???? ???? ???? ???? ????
15
(e) 55?1??,??1
a 0 1 2 3 4 5 6 7
? 47 40 25 30 6 12 1 55
????  ????  ????  ????  ????  ????  ????  ????  ???? 
16
(f)47?1??,??1
a 0 1 2 3 4 5 6
? 40 30 25 1 6 12 47
???? ???? ???? ???? ???? ???? ???? ????
17
(g) 40?12??,??12
a 0 1 2 3 4  5
? 30 12 25 1 6  40
???? ???? ???? ???? ???? ???? ????
18
(h)6?30??,??6
a 0 1 2 3 4
? 25 12 6 1 30
???? ???? ???? ???? ???? ????
19
(i) 25?1??,??1
a 0 1 2 3
? 12 1 6 25
???? ???? ???? ???? ????
20
(j)12?6??
a 0 1 2
? 6 1 6
???? ???? ???? ????
21
???
  • gtC?????
  • move_down(int a , int from, int to)
  • int i, j, t
  • i from
  • while ( i lt to /2 )
  • j i 2 1
  • if ( j 1 lt to a j lt a j 1 )
  • j
  • if (a i lt a j )

22
  • t a i
  • a i a j
  • a j t
  • i j

23
  • heap_sort(int a , int n )
  • int i, t
  • for ( i n /2 1 i gt 0 i - -)
  • move_down(a, i, n)
  • for ( i n 1 i gt 0 i - -)
  • t a 0
  • a 0 a i
  • a i t
  • move_down ( a, 0, i )

24
  • gt???????
  • ???????????,
  • ??????????????O(nlogn)?
  • ??????????

25
????-????
  • include ltstudio.hgt
  • void adjust ( int , int )
  • int data 11 0, 75, 23, 98, 44, 57, 12, 29,
    64, 38, 82
  • void main ( )
  • int i , k , temp
  • printf ( \n ltlt Heap sort gtgt\n )
  • printf ( Number )
  • for ( k 1 k lt 10 k )
  • printf ( d , data k )
  • puts ( )for ( k 0 k lt 60 k ) printf (
    - )

26
  • for ( i 10 / 2 i gt 0 i --)
  • adjust ( i, 10 )
  • printf ( \nHeap )
  • for ( k 1 k lt 10 k )
  • printf ( d , data k )
  • for ( i 9 i gt 0 i --)
  • temp data i1
  • data i1 data 1

27
  • data 1 temp
  • adjust (1, i )
  • printf ( \nAccess )
  • for ( k 1 k lt 60 k )
  • printf ( d , data k )
  • puts ( )
  • for ( k 0 k lt 60 k ) printf ( - )
  • printf ( \nSorting )
  • for ( k 1 k lt 60 k )

28
  • void adjust ( int i, int n )
  • int j, k, dome 0
  • k data i
  • j 2 i
  • while (( j lt n ) ( done 0 ))
  • if (( j lt n ) ( data j lt data j 1
    )) j
  • if (( j lt n ) done 1

29
  • else
  • data j / 2 data j
  • j 2
  • data j / 2 k
Write a Comment
User Comments (0)
About PowerShow.com