Vous êtes sur la page 1sur 5

11/4/13

Heap Sort - GeeksQuiz | GeeksQuiz

GeeksQuiz
Computer science mock tests for geeks Home Latest Questions Articles Subscribe

Heap Sort
March 16, 2013 Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element. What is Binary Heap? Let us first define a Complete Binary Tree. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible (Source Wikipedia) A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a parent node is greater(or smaller) than the values in its two children nodes. The former is called as max heap and the latter is called min heap. The heap can be represented by binary tree or array. Why array based representation for Binary Heap? Since a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based representation is space efficient. If the parent node is stored at index I, the left child can be calculated by 2 * I + 1 and right child by 2 * I + 2. Heap Sort Algorithm for sorting in increasing order: 1. Build a max heap from the input data. 2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree.
geeksquiz.com/heap-sort/ 1/8

11/4/13

Heap Sort - GeeksQuiz | GeeksQuiz

3. Repeat above steps until size of heap is greater than 1. How to build the heap? Heapify procedure can be applied to a node only if its children nodes are heapified. So the heapification must be performed in the bottom up order. Lets understand with the help of an example:
I n p u td a t a :4 ,1 0 ,3 ,5 ,1 4 ( 0 ) / \ 0 ( 1 ) 3 ( 2 ) / \ 5 ( 3 ) 1 ( 4 ) T h en u m b e r si nb r a c k e tr e p r e s e n tt h ei n d i c e si nt h ea r r a yr e p r e s e n t a t i o no fd a t a . A p p l y i n gh e a p i f yp r o c e d u r et oi n d e x1 : 4 ( 0 ) / \ 1 0 ( 1 ) 3 ( 2 ) / \ 5 ( 3 ) 1 ( 4 ) A p p l y i n gh e a p i f yp r o c e d u r et oi n d e x0 : 1 0 ( 0 ) / \ 5 ( 1 ) 3 ( 2 ) / \ 4 ( 3 ) 1 ( 4 ) T h eh e a p i f yp r o c e d u r ec a l l si t s e l fr e c u r s i v e l yt ob u i l dh e a pi nt o pd o w nm a n n e r .

/ /Ci m p l e m e n t a t i o no fH e a pS o r t # i n c l u d e< s t d i o . h > # i n c l u d e< s t d l i b . h > / /Ah e a ph a sc u r r e n ts i z ea n da r r a yo fe l e m e n t s s t r u c tM a x H e a p { i n ts i z e ; i n t *a r r a y ; } ; / /Au t i l i t yf u n c t i o nt os w a pt oi n t e g e r s


geeksquiz.com/heap-sort/ 2/8

11/4/13

Heap Sort - GeeksQuiz | GeeksQuiz

v o i ds w a p ( i n t *a ,i n t *b ){i n tt=* a ;* a=* b ; * b=t ;} / /T h em a i nf u n c t i o nt oh e a p i f yaM a xH e a p .T h ef u n c t i o na s s u m e st h a t / /e v e r y t h i n gu n d e rg i v e nr o o t( e l e m e n ta ti n d e xi d x )i sa l r e a d yh e a p i f i e d v o i dm a x H e a p i f y ( s t r u c tM a x H e a p *m a x H e a p ,i n ti d x ) { i n tl a r g e s t=i d x ; / /I n i t i a l i z el a r g e s ta sr o o t i n tl e f t=( i d x< <1 )+1 ; / /l e f t=2 * i d x+1 i n tr i g h t=( i d x+1 )< <1 ;/ /r i g h t=2 * i d x+2 / /S e ei fl e f tc h i l do fr o o te x i s t sa n di sg r e a t e rt h a nr o o t i f( l e f t<m a x H e a p > s i z e& &m a x H e a p > a r r a y [ l e f t ]>m a x H e a p > a r r a y [ l a r g e s t ] ) l a r g e s t=l e f t ; / /S e ei fr i g h tc h i l do fr o o te x i s t sa n di sg r e a t e rt h a nt h el a r g e s ts of a r i f( r i g h t<m a x H e a p > s i z e& &m a x H e a p > a r r a y [ r i g h t ]>m a x H e a p > a r r a y [ l a r g e s t ] ) l a r g e s t=r i g h t ; / /C h a n g er o o t ,i fn e e d e d i f( l a r g e s t! =i d x ) { s w a p ( & m a x H e a p > a r r a y [ l a r g e s t ] ,& m a x H e a p > a r r a y [ i d x ] ) ; m a x H e a p i f y ( m a x H e a p ,l a r g e s t ) ; }

/ /Au t i l i t yf u n c t i o nt oc r e a t eam a xh e a po fg i v e nc a p a c i t y s t r u c tM a x H e a p *c r e a t e A n d B u i l d H e a p ( i n t* a r r a y ,i n ts i z e ) { i n ti ; s t r u c tM a x H e a p *m a x H e a p=( s t r u c tM a x H e a p * )m a l l o c ( s i z e o f ( s t r u c tM a x H e a p ) ) ; m a x H e a p > s i z e=s i z e ; / /i n i t i a l i z es i z eo fh e a p m a x H e a p > a r r a y=a r r a y ;/ /A s s i g na d d r e s so ff i r s te l e m e n to fa r r a y / /S t a r tf r o mb o t t o m m o s ta n dr i g h t m o s ti n t e r n a lm o d ea n dh e a p i f ya l l / /i n t e r n a lm o d e si nb o t t o mu pw a y f o r( i=( m a x H e a p > s i z e-2 )/2 ;i> =0 ;i ) m a x H e a p i f y ( m a x H e a p ,i ) ; r e t u r nm a x H e a p ;
3/8

geeksquiz.com/heap-sort/

11/4/13

Heap Sort - GeeksQuiz | GeeksQuiz

/ /T h em a i nf u n c t i o nt os o r ta na r r a yo fg i v e ns i z e v o i dh e a p S o r t ( i n t *a r r a y ,i n ts i z e ) { / /B u i l dah e a pf r o mt h ei n p u td a t a . s t r u c tM a x H e a p *m a x H e a p=c r e a t e A n d B u i l d H e a p ( a r r a y ,s i z e ) ; / /R e p e a tf o l l o w i n gs t e p sw h i l eh e a ps i z ei sg r e a t e rt h a n1 .T h el a s t / /e l e m e n ti nm a xh e a pw i l lb et h em i n i m u me l e m e n t w h i l e( m a x H e a p > s i z e>1 ) { / /T h el a r g e s ti t e mi nH e a pi ss t o r e da tt h er o o t .R e p l a c ei tw i t ht h e / /l a s ti t e mo ft h eh e a pf o l l o w e db yr e d u c i n gt h es i z eo fh e a pb y1 . s w a p ( & m a x H e a p > a r r a y [ 0 ] ,& m a x H e a p > a r r a y [ m a x H e a p > s i z e-1 ] ) ; m a x H e a p > s i z e ; / /R e d u c eh e a ps i z e / /F i n a l l y ,h e a p i f yt h er o o to ft r e e . m a x H e a p i f y ( m a x H e a p ,0 ) ;

/ /Au t i l i t yf u n c t i o nt op r i n tag i v e na r r a yo fg i v e ns i z e v o i dp r i n t A r r a y ( i n t *a r r ,i n ts i z e ) { i n ti ; f o r( i=0 ;i<s i z e ;+ + i ) p r i n t f ( " % d" ,a r r [ i ] ) ; } / *D r i v e rp r o g r a mt ot e s ta b o v ef u n c t i o n s* / i n tm a i n ( ) { i n ta r r [ ]={ 1 2 ,1 1 ,1 3 ,5 ,6 ,7 } ; i n ts i z e=s i z e o f ( a r r ) / s i z e o f ( a r r [ 0 ] ) ; p r i n t f ( " G i v e na r r a yi s\ n " ) ; p r i n t A r r a y ( a r r ,s i z e ) ; h e a p S o r t ( a r r ,s i z e ) ;
geeksquiz.com/heap-sort/ 4/8

11/4/13

Heap Sort - GeeksQuiz | GeeksQuiz

p r i n t f ( " \ n S o r t e da r r a yi s\ n " ) ; p r i n t A r r a y ( a r r ,s i z e ) ; r e t u r n0 ;

Output:
G i v e na r r a yi s 1 21 11 3567 S o r t e da r r a yi s 5671 11 21 3

Notes: Heap sort is an in-place algorithm. Its typical implementation is not stable, but can be made stable (See this) Time Complexity: Time complexity of heapify is O(Logn). Time complexity of createAndBuildHeap() is O(n) and overall time complexity of Heap Sort is O(nLogn). Applications of HeapSort 1. Sort a nearly sorted (or K sorted) array 2. k largest(or smallest) elements in an array Heap sort algorithm has limited uses because Quicksort and Mergesort are better in practice. Nevertheless, the Heap data structure itself is enormously used. See Applications of Heap Data Structure Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Like

Tw eet

Writing code in comment? Please refer here for guidelines.

geeksquiz.com/heap-sort/

5/8

Vous aimerez peut-être aussi