HEAPSORT ALGORITHM

HEAPSORT(A)

  1. BUILD_MAX_HEAP(A)
  2. for i<- length [A] down to 2
  3. do exchange A[1] <-> A[i]
  4. heapsize[A]<- heapsize[A] – 1
  5. MAX_HEAPIFY(A,1)

BUILD_MAX_HEAP(A)

  1. heapsize[A] <- length [A]
  2. for i <- length [A]/2 down to 1
  3. do MAX_HEAPIFY (A,i)

MAX_HEAPIFY(A,i)

  1. l <- left [i]
  2. r <- right[i]
  3. if l <= heapsize [A] & A[l] > A[i]
  4. then largest <- l
  5. else largest <- i
  6. if r <= heapsize[A] and A[r] > A[largest ]
  7. then largest <- r
  8. if largest !=i
  9. then exchange A[i] <-> A[largest]
  10. MAX_HEAPIFY (A,largest)

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *