Heapsort is an O(n log n) sorting algorithm that uses a heap data structure. It works by first turning the input array into a max heap, where the largest element is stored at the root. It then repeatedly removes the root element and replaces it with the last element of the heap, and sifts it down to maintain the heap property. This produces the sorted array from largest to smallest. The heapify and reheap operations each take O(log n) time, and are performed n times, resulting in an overall time complexity of O(n log n).