Heap Sort - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Heap Sort

Description:

Heap Sort. . MaxHeap . ... – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 17
Provided by: hpk5
Category:
Tags: heap | sort

less

Transcript and Presenter's Notes

Title: Heap Sort


1
???? ???? ????
Heap Sort
???? ??????? ???? ????? ? ??? ???? ????? 88
2
Heap Sort
?? ????. MaxHeap?? ??? ???? ?????? ?? ?? ????? ?
???? ??? ?? ????
????? ?????? ? ???? ??? ?? ????? ????? ??? ?? ??
??? ? MaxHeap( ???? ?????? ? ?? ????? ????? ?????
??? ?? ??? ???????? ???)
??? ????? ??????? ?? ?? ???? ??? ????? ? ?? ?? ??
?????? ????? ??????? .?????? MaxHeap ??????? ??
???? ????? ?? ????
? ??? ??? ????? ?? ??? ?? ??? ????? ????? ??
????? ??? ???? ???? ???.
???? ????? ? ????? ???? ??? ???? ??? ?????
??????. Heap ??? ??? ?? ????? ?? ??? ?????? ??
???. ?? ??? ??? ????? ?? ??? ?? ??? ???? ?? ???.
O(nlogn) ????? ??????? ??? ????????
3
???? ??? ?? ?? ?????...
??? ?????? ???? ???? ???? ?? ????? ?? ???? ?? ???
? ???? ???.
?? ??? ????? ????????? ??????? ?? ??? ???? ????
???? ?? ??????? ?? ??????? ?? ?? ?? ??? ????.
??? ???? ???? ?????? ???? ???? ???? ?? ?? ??? ??
????? ???? ???????.
??? ??? ???? ???? ?????? ???? ???? ???? ?? ??
???? ???? ??????? ??????.
?????? ???? ???? ???? ?? ???? ??????? ???? ? ??
??? ????? ????? ?? ?????? ??? ????.
4
?????? ???? ???? Heap ?? ???
?? Min-Heap?
??Max-Heap
5
??? ??? ?? Max-Heap ?????
????? ?? ??? ?????? ????? ????? ???? ???? ?? ????
????? ??????. Max-Heap ???????? ???????? ???
  APARENT(i)gtAi ?? ???? ??   PARENT(i) retur
n i/2
?? ???? ?? ??? ??? ???????. Min-Heap
????? ???????? ??? ?? ???? ???? ???????.
6
??????? ?????. Max-Heap ?? ??? ???????? ???? ????
??
  ( HEAPSORT(A (BIULD-MAX-HEAP(A (for(ilength
A downto 2 do swap(A1,Ai )
Heap-sizeAHeap-sizeA-1 (MAX-HEAPIFY(A,1
..ltlt ???? ????gtgt ..
?? ??? ?????? O(n logn ????(HEAPSORT ????
?O(n) ???? BUILD-HEAP ??? ????????
?? ?? ?? n-1 ???????? MAX-HEAPIFY ???? (O(logn
?? ??? ??????.
7
Heap Sort ????????
  //array of integers to hold values private
int a new int100   number of elements in
array// private int x   //Heap Sort
Algorithm public void sortArray() int I
int temp   (--for( i (x/2)-1 i gt 0 i
8
(siftDown( i, x   (--for( i x-1 i gt
1 i temp a0 a0 ai ai
temp (siftDown( 0, i-1   public void
siftDown( int root, int bottom) bool done
false int maxChild int temp   ((while(
(root2 lt bottom) (!done (if( root2
bottom maxChild root 2
9
else if( aroot 2 gt aroot 2 1) maxChild
root 2 else maxChild root 2 1
  if( aroot lt amaxChild) temp
aroot aroot amaxChild amaxChild
temp root maxChild else done
true
10
???? ??? ?? ?? ?????...
) ???????? min-heap(?? max-heap ?? ?? ????
(?? ????????) ???? ?????? ?? ???? ???? ????. ??
??? ??? ???? ??? ?? ???? ?????? (?? ??????) ???
????? ???? ????? ?? ???? ???? ?? ????.
)min-heap (?? max-heap ?? ????? ?? ???? ?? ???
????? ??? ??? ????
???? ?? ?? ?? ???? ????? (?? ?????) ? ???? ??? ??
??? ????.
?? ????? ????
11
Step 0 )   min-heap 1 , 4 , 5 , 8 , 6 , 9    
-    array Step 1 )   min-heap 4 , 6 , 5 , 8 ,
9          -    array 1 Step 2 )   min-heap 5 ,
6 , 9 , 8               -    array 1 , 4 Step 3
)   min-heap 6 , 8 , 9                    -   
array 1 , 4 , 5 Step 4 )   min-heap 8 ,
9                         -    array 1 , 4 , 5 ,
6 Step 5 )   min-heap 9                          
    -    array 1 , 4 , 5 , 6 , 8 Step 6 )  
min-heap                                 -   
array 1 , 4 , 5 , 6 , 8 , 9
??? ??? ??? ?? ???? ??? n ??????? ?? ?????? ??
???? ??
????? ?? ???? ???? ??? ?? ??? ?? ????. ???.   O(
log2n) ?? ????? Heap ??? ??? ?? ????
????? ?? ??? ???? ???? ???? n ?? ????? ????? ????
???? ?????
????? ???.  O ( n log2n(
??? ??? ?? ???? ???? ???? ?? ???? ??? ?????
??? ????? n ?? ?? ???? ????? ???? ???? ????
?????????. ?? ????? ???? ???? ???? ????
? heap ???? ????? ?? ??? ????? ?? ?? ??? ???? n
?? ????? ?????
????? ????? ???? ??? ?? ??? ?? ?????. ???? ?? ???
???? ???? ??? ?? ????? ????? ?? ?? ?? ?? ????
?????? ?? ?? ????? ????? ????! ??? ??????
12
??? ?? ?? ??? ?????? Min-heap ????
  ?? ??? ???? ???? ? ????? ??????? ???? ?? ?????
??? ?? ????
??? ???? ?????? ???? ?? ???? ?? ???. ???? ?????
6 ???? ??????? ????? ? ??? ??????? ???!
13
???? ??? ??? ?? ?? ???? ???? ?? ????
???? ????? ????? ?? ?? ??? ???? ???? ?? ?? ???
???? ????? ??? ??? ? ???? ??? ????? ??? ???
????. ?? ???? ????? ?? ??? ???? ??? ???? ?????
14
? ?? ??? ???? ??? ??? ?? ??? ??? ???????
? ?? ???? ????? ?? ??? ???? ?? ? ??? ???? ?? ???
??? ??? ??????? ?? ???? ???? ?? ????? ??? ??????
????
?? ????? ?? ?? ???? ???? ??? ?? ?? ?? ???. ?????
???? ????? ????? ?? ?? ???? ????? ????? ??? ???
)?? ?? ??? ???? ????? ?? ?? ???? ????? ?? ???
??????. ???? ?? ??? ??? ?? ???? Step 6 (
???? ???? ???? ????? ??????? ?? ???. Max-heap
???? ???? ???? ????? ? ?? ???? Min-heap
????? ?? ????? ???? ???? ??? ???? ????? ??? ???
?????? ??? ????? ????? ???.
15
?? ??? ???? ????? ??? C ?? ???? ??????? ??? ????
???? ???? ???? ?? ????
  void heapsort ( int array , int n )   int
m 0   while ( m lt n )     push ( array, m,
array m 1 )   while ( m )     array m
1 pop ( array, m )
16
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com