Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A Regarding the heap sort algorithm.

As you know, a max heap is a binary tree such that each parent is greater than its children. The most compact and efficient way to represent such a tree is an array, where each index corresponds t...

posted 2y ago by meriton‭  ·  edited 2y ago by meriton‭

Answer
#3: Post edited by user avatar meriton‭ · 2021-12-20T00:36:54Z (over 2 years ago)
  • As you know, a max heap is a binary tree such that each parent is greater than its children.
  • The most compact and efficient way to represent such a tree is an array, where each index corresponds to a node in the tree, numbered from the root, level by level, from left to right, as shown in the following picture:
  • 0
  • / \
  • / \
  • 1 2
  • / \ / \
  • 3 4 5 6
  • Then, if a node is stored at index `i`, we know that its left child is stored at index `i * 2 + 1`, and its right child at index `i * 2 + 2`.
  • This representation has the advantage that we do not need any pointers to represent the structure of the tree, allowing for a more compact memory layout.
  • The sort proceeds by taking the numbers in an array, reinterpreting that array as an encoded max heap, and moving elements around until the heap invariant (that each parent is greater than both of its children) is satisfied.
  • The heapify function takes an index i, and checks that the node stored there is greater than its children. If this is not the case, the greater child swaps places with the parent. Since this swap reduces the value of the child, it is possible that the child is no longer greater than its children, so we recursively check them (again).
  • More precisely, the heapify function assumes that the subtree of the children are correct (each node greater than its children), and makes sure the entire tree rooted at `i` is correct. By invoking heapify for each node, from bottom to top, we therefore ensure the entire tree is correct.
  • And that's why the function is called `heapify`: It turns our unsorted array into an encoded max heap.
  • As you know, a max heap is a binary tree such that each parent is greater than its children.
  • The most compact and efficient way to represent such a tree is an array, where each index corresponds to a node in the tree, numbered from the root, level by level, from left to right, as shown in the following picture:
  • 0
  • / \
  • / \
  • 1 2
  • / \ / \
  • 3 4 5 6
  • Then, if a node is stored at index `i`, we know that its left child is stored at index `i * 2 + 1`, and its right child at index `i * 2 + 2`.
  • This representation has the advantage that we do not need any pointers to represent the structure of the tree, allowing for a more compact memory layout.
  • The sort proceeds by taking the numbers in an array, reinterpreting that array as an encoded max heap, and moving elements around until the heap invariant (that each parent is greater than both of its children) is satisfied.
  • The heapify function takes an index i, and checks that the node stored there is greater than its children. If this is not the case, the greater child swaps places with the parent. Since this swap reduces the value of the child, it is possible that the child is no longer greater than its children, so we recursively check them (again).
  • More precisely, the heapify function assumes that the subtrees of the children are correct (each node greater than its children), and makes sure the entire tree rooted at `i` is correct. By invoking heapify for each node, from bottom to top, we therefore ensure the entire tree is correct.
  • And that's why the function is called `heapify`: It turns our unsorted array into an encoded max heap.
#2: Post edited by user avatar meriton‭ · 2021-12-20T00:21:01Z (over 2 years ago)
  • As you know, a max heap is a binary tree such that each parent is greater than its children.
  • The most compact and efficient way to represent such a tree is an array, where each index corresponds to a node in the tree, numbered from the root, level by level, from left to right, as shown in the following picture:
  • 0
  • / \
  • / \
  • 1 2
  • / \ / \
  • 3 4 5 6
  • Then, if a node is stored at index `i`, we know that its left child is stored at index `i * 2 + 1`, and its right child at index `i * 2 + 2`.
  • This representation has the advantage that we do not need any pointers to represent the structure of the tree, allowing for a more compact memory layout.
  • The sort proceeds by taking the numbers in an array, reinterpreting that array as an encoded max heap, and moving elements around until the heap invariant (that each parent is greater than both of its children) is satisfied.
  • The heapify function takes an index i, and checks that the node stored there is greater than its children. If this is not the case, the greater child swaps places with the parent. Since this swap reduces the value of the child, it is possible that the child is no longer greater than its children, so we recursively check them (again).
  • More precisely, the heapify function assumes that the subtree of the children are correct (each node greater than its children), and makes sure the entire tree rooted at `i` is correct. By invoking heapify for each node, from bottom to top, we therefore ensure the entire tree is correct.
  • As you know, a max heap is a binary tree such that each parent is greater than its children.
  • The most compact and efficient way to represent such a tree is an array, where each index corresponds to a node in the tree, numbered from the root, level by level, from left to right, as shown in the following picture:
  • 0
  • / \
  • / \
  • 1 2
  • / \ / \
  • 3 4 5 6
  • Then, if a node is stored at index `i`, we know that its left child is stored at index `i * 2 + 1`, and its right child at index `i * 2 + 2`.
  • This representation has the advantage that we do not need any pointers to represent the structure of the tree, allowing for a more compact memory layout.
  • The sort proceeds by taking the numbers in an array, reinterpreting that array as an encoded max heap, and moving elements around until the heap invariant (that each parent is greater than both of its children) is satisfied.
  • The heapify function takes an index i, and checks that the node stored there is greater than its children. If this is not the case, the greater child swaps places with the parent. Since this swap reduces the value of the child, it is possible that the child is no longer greater than its children, so we recursively check them (again).
  • More precisely, the heapify function assumes that the subtree of the children are correct (each node greater than its children), and makes sure the entire tree rooted at `i` is correct. By invoking heapify for each node, from bottom to top, we therefore ensure the entire tree is correct.
  • And that's why the function is called `heapify`: It turns our unsorted array into an encoded max heap.
#1: Initial revision by user avatar meriton‭ · 2021-12-20T00:19:15Z (over 2 years ago)
As you know, a max heap is a binary tree such that each parent is greater than its children.

The most compact and efficient way to represent such a tree is an array, where each index corresponds to a node in the tree, numbered from the root, level by level, from left to right, as shown in the following picture:

                0
               / \
              /   \
             1     2
            / \   / \
           3   4 5   6
          
Then, if a node is stored at index `i`, we know that its left child is stored at index `i * 2 + 1`, and its right child at index `i * 2 + 2`.

This representation has the advantage that we do not need any pointers to represent the structure of the tree, allowing for a more compact memory layout. 

The sort proceeds by taking the numbers in an array, reinterpreting that array as an encoded max heap, and moving elements around until the heap invariant (that each parent is greater than both of its children) is satisfied. 

The heapify function takes an index i, and checks that the node stored there is greater than its children. If this is not the case, the greater child swaps places with the parent. Since this swap reduces the value of the child, it is possible that the child is no longer greater than its children, so we recursively check them (again).

More precisely, the heapify function assumes that the subtree of the children are correct (each node greater than its children), and makes sure the entire tree rooted at `i` is correct. By invoking heapify for each node, from bottom to top, we therefore ensure the entire tree is correct.