mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	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:
		@ -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>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user