0% found this document useful (0 votes)
24 views2 pages

Heap Sort

Heap sort in data structure tree sorting unit 4

Uploaded by

sahoobiswojit165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Heap Sort

Heap sort in data structure tree sorting unit 4

Uploaded by

sahoobiswojit165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
7 SEARCHING AND SORTING HEAP SORT 7 + A heap can be defined as a binary tree with one key (value) @ assigned to each node provided the following two requirements are satisfied IO) (10) 1, Tree shape - the binary tree is essential ly complete; all levels are full except possibly the last level where only some @ © rightmost leaves are missing. 2. Parental dominance - the key at each node is greater than QO or equals the keys of the children; this is satisfied automatically for leaf nodes. This is also called maxheap. + Let Root node at A(O} in the array a, If Parent is at ACI], then Left child of At will be at AL2T+1] and Right child of A(z] willbe ata2z + 2). For example, to represent the given heap in the array as Follows w[mTitTetT7tTets[2[T4{[1) oi 2 #3 4 5 6 7 8 9 MAINTAINING THE HEAP PROPERTY + Heapify is an import subroutine for manipulating the heaps. + Its inputs are an array A with number of elements N and one index I. = When heapity is called, it is assumed that the binary trees rooted at x with left child at 22+2 and right child at 21+2 are heap. = Butthe A[Z) may be smaller than its children, thus violating the heap property. = The procedure heapify is to let the value at A(11, float down in the heap so thal the subtree rooted at index T become heap. HEAPIFY(A, |, N): Here the array is A with number of elements Wand the index is 2. This algorithm creates a heap rooted at index Z of array A. 1. Set Le2e141 2. Set Ra2*i+2 3. If L <= N-1 and A[L] > AIT], then: Set GST =L Else: sot Gst = I [ind of step 2 if structure} 4, If R <= Nel and AIR] > A[GST] ‘set GST = [End of stop 4 if structure) 5. If GST # I, then: ‘a, Interchange A[T] and A(GST] b. HEAPIFY (A,GS7,N) (end of step 5 if structure] 6. Exit CREATING HEAP = We can use the procedure HEAPZ=Y in a bottom-up manner to convert the array A with length X into.a heap. Since the elements in the suborray A(N/21..A{N-1] are all leaves of the tree or each is a single element heap to begin with. The procedure CRATEHEAP goes through the remaining nodes of the tree runs HEAPZEY on each one, ‘The order in which the nodes are processed guarantees that the subtrees rooted al children of anode T are heaps before HEAPZEY is run at that node. 7 Recon: CREATEHEAP(A.N):Here the array A with the number of elements N. This algorithm creates the hea} 1 Repeat step 2 for T= (-1)/2 to 0 by 2. call Heapify (A,,N) : {end of step 1 1e0P) 3. Exit 815 ¥ ‘ShreeSoft THE HEAPSORT ALGROTHM : Ps ith + The algorithm starts by using CREATEHEAP to build a heap in the input array with number er elements ¥. i. the root of the heap. + Since the greatest element of the array is stored at A{0} i.e. become the 2. its final position. + After interchanging the A[0] and A[¥-1] , the root of the heap got Se a * Now discard the node w-1 from the heap by decreasing the heap a eee +The children of the root remain heaps, but the new root element may heap coe o,N-2)- + Tomaintain the heap property again call the procedure HEAPIE (A acne +The HEAPSOR® algorithm then repeals this process for the heap of Size 1 RITHM: jen elements in ascending order by using the procedure CREATEHEAP and HEAP: 1. CREATEHEAP(A,N) 2. Repeat Steps 3 and 4 for I=N-1 to 1 by -1 | 3 Interchange A[0] and A(I] a Call HEAPIFY (A,0,1) i (End of Step 2 loop] { 5. Exit C PROGRAM FOR HEAP SORT #include void heapsort (int a{},int =) #include { void awap(int *x,int *y) int i,ty (ant ty exeateheap (2,n) 7 for (ien-27 ‘ swap (ea[0] ,¢ali])7 ; heapity (2,0, void heapify(int af],int i,int n) ) ‘ ) int 1,r,gst,t; int main() a int a[100] ,n,i; printf ("Enter nusber of elenents:"); scant ("8d", én); printé ("Enter %d elexent ee a(1}>alil) if(zeon-1. £6 a[r]>algst]) get=r; Af (gst! (swap (Ga[i},6afget}) ; ) haapsty (a, g0t,n) ; ; printf ("The sorted array is :"); void createheap(int peer) . ap(int al], int np printe("td\e",ali}) ; for (i=0;i=0;4: heapity(a,iin) return 0; d ANALYSIS OF HEAPSORT + Suppose H is the heap. +The number of comparisons to fi heap H cannot exceed the depth of H. Porepriate Place of a new element in the + His a complete binary ree an id its depth is ‘elements in H. ‘S depth is bounded by loge n, where n is the number of

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy