chap internal sorting original numbers build heap remove remove remove figure an illustration of heapsort the top row shows the values in their original order the second row shows the values after building the heap the third row shows the result of the first removefirst operation on key value note that is now at the end of the array the fourth row shows the result of the second removefirst operation on key value the fifth row shows the result of the third removefirst operation on key value at this pointthe last three positions of the array hold the three greatest values in sorted order heapsort continues in this manner until the entire array is sorted