input: Write a python function to perform heap sort using the MaxHeap class.
  assert MaxHeap([3, 2, 1, 4, 5]).heapsort([3, 2, 1, 4, 5]) == [1, 2, 3, 4, 5]
  assert MaxHeap([9, 7, 8, 5, 6, 3, 2]).heapsort([9, 7, 8, 5, 6, 3, 2]) == [2, 3, 5, 6, 7, 8, 9]
  assert MaxHeap([10, 20, 15, 30, 40]).heapsort([10, 20, 15, 30, 40]) == [10, 15, 20, 30, 40]
output: class MaxHeap:
      def __init__(self, elements):
          self.heap = elements
          self.build_max_heap()
  
      def build_max_heap(self):
          for i in range(len(self.heap) // 2 - 1, -1, -1):
              self.max_heapify(i)
  
      def max_heapify(self, i):
          largest = i
          left = 2 * i + 1
          right = 2 * i + 2
          if left < len(self.heap) and self.heap[left] > self.heap[largest]:
              largest = left
          if right < len(self.heap) and self.heap[right] > self.heap[largest]:
              largest = right
          if largest != i:
              self.heap[i], self.heap[largest] = self.heap[largest], self.heap[i]
              self.max_heapify(largest)
  
      def heapsort(self, elements):
          self.heap = elements
          self.build_max_heap()
          sorted_array = []
          for _ in range(len(self.heap)):
              self.heap[0], self.heap[-1] = self.heap[-1], self.heap[0]
              sorted_array.insert(0, self.heap.pop())
              self.max_heapify(0)
          return sorted_array