Translation: Update heap_sort.md (#1631)

* Translation: Update heap_sort.md

* Translation: Update heap_sort.md

updated per yuelinxin's suggestion.
This commit is contained in:
K3v123 2025-02-10 15:35:18 +13:00 committed by GitHub
parent 3d12f84a01
commit 387f93b56f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,27 +2,27 @@
!!! tip
Before reading this section, please make sure you have completed the "Heap" chapter.
Before reading this section, please ensure you have completed the "Heap" chapter.
<u>Heap sort</u> is an efficient sorting algorithm based on the heap data structure. We can implement heap sort using the "heap creation" and "element extraction" operations we have already learned.
1. Input the array and establish a min-heap, where the smallest element is at the heap's top.
2. Continuously perform the extraction operation, recording the extracted elements in sequence to obtain a sorted list from smallest to largest.
1. Input the array and construct a min-heap, where the smallest element is at the top of the heap.
2. Continuously perform the extraction operation, record the extracted elements sequentially to obtain a sorted list from smallest to largest.
Although the above method is feasible, it requires an additional array to save the popped elements, which is somewhat space-consuming. In practice, we usually use a more elegant implementation.
Although the above method is feasible, it requires an additional array to store the popped elements, which is somewhat space-consuming. In practice, we usually use a more elegant implementation.
## Algorithm flow
Suppose the array length is $n$, the heap sort process is as follows.
1. Input the array and establish a max-heap. After completion, the largest element is at the heap's top.
2. Swap the top element of the heap (the first element) with the heap's bottom element (the last element). After the swap, reduce the heap's length by $1$ and increase the sorted elements count by $1$.
1. Input the array and establish a max-heap. After this step, the largest element is positioned at the top of the heap.
2. Swap the top element of the heap (the first element) with the heap's bottom element (the last element). Following this swap, reduce the heap's length by $1$ and increase the sorted elements count by $1$.
3. Starting from the heap top, perform the sift-down operation from top to bottom. After the sift-down, the heap's property is restored.
4. Repeat steps `2.` and `3.` Loop for $n - 1$ rounds to complete the sorting of the array.
!!! tip
In fact, the element extraction operation also includes steps `2.` and `3.`, with the addition of a popping element step.
In fact, the element extraction operation also includes steps `2.` and `3.`, with an additional step to pop (remove) the extracted element from the heap.
=== "<1>"
![Heap sort process](heap_sort.assets/heap_sort_step1.png)