An intuitive understanding of heapsort?

后端 未结 7 2199
春和景丽
春和景丽 2021-01-30 00:18

At school we are currently learning sorting algorithms in Java and I got for my homework the Heap Sort. I did my reading, I tried to find out as much as I could, but it seems I

7条回答
  •  情深已故
    2021-01-30 00:42

    I remember my Algorithm analysis professor to tell us that the heap sort algorithm works like a heap of gravel:

    Imagine you have a sack filled with gravel, and you empty that on the floor: Bigger stones will likely roll to the bottom and smaller stones (or sand) will stay on top.

    You now take the very top of the heap and save it at the smallest value of your heap. Put again the rest of your heap in your sack, and repeat. (Or you can get the opposite approach and grab the biggest stone you saw rolling on the floor, the example is still valid)

    That's it more or less the simple way i know to explain how heap sort works.

提交回复
热议问题