Skip to content

Commit

Permalink
Edit heap section
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Jan 3, 2020
1 parent f6d1bbb commit c3738ba
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
Expand Up @@ -55,7 +55,7 @@ Merging two arrays like this uses a third array to store the results, which must

_Note: It's possible to implement merge sort in an array without using much extra storage (see Kronrod's algorithm) {% cite algorithm-design-manual -l 138 %}._

Merge sort runs in $$O(n\log n)$$ {% cite algorithm-design-manual -l 122 %} in the worst-case.
Merge sort runs in $$O(n\lg n)$$ {% cite algorithm-design-manual -l 122 %} in the worst-case.

The following code implements merge sort:

Expand Down
6 changes: 3 additions & 3 deletions docs/data-structures-and-algorithms/data-structures/heap.md
Expand Up @@ -23,15 +23,15 @@ permalink: /data-structures-and-algorithms/data-structures/heap

## Introduction

Heaps are a data structure for supporting the priority queue operations `insert()` and `extract_min()`. Heaps maintain a partial order of elements that is weaker than sorted order, but stronger than random order {% cite algorithm-design-manual -l 109 %}.
A heap is a data structure for supporting the priority queue operations `insert()` and `extract_min()`. Heaps maintain a partial order of elements that is weaker than sorted order, but stronger than random order {% cite algorithm-design-manual -l 109 %}.

A heap-labelled tree is a binary tree where the key labelling of each node _dominates_ each of its children. In a min-heap, a node dominates its children by holding a smaller key than its children. In a max-heap tree, a node dominates its children by holding a larger key than its children {% cite algorithm-design-manual -l 110 %}.

In a min-heap tree, the root node is always the node with the lowest value.

## Representing a heap

A heap can be represented as an array, rather than using pointers like a typical binary tree. The position of the keys in the array is used to determine the position of the key in the tree {% cite algorithm-design-manual -l 110 %}.
A heap can be represented as an array, rather than using pointers like a typical binary tree. The position of keys in the array is used to determine the position of keys in the tree {% cite algorithm-design-manual -l 110 %}.

The root of the tree is the first position in the array. Its left and right sub children are stored at position 2 and 3 respectively {% cite algorithm-design-manual -l 110 %}.

Expand Down Expand Up @@ -105,7 +105,7 @@ void bubble_up(priority_queue *q, int p) {

{% cite algorithm-design-manual -l 112 %}

The swap takes constant time at each level. Since the height of a heap is $$\log n$$, the max insert will take $$O(\log n)$$. In other words, a heap of $$n$$ elements can be constructed in $$O(n\log n)$$ time:
The swap takes constant time at each level. Since the height of a heap is $$\lg n$$, the max insert will take $$O(\log n)$$. In other words, a heap of $$n$$ elements can be constructed in $$O(n\log n)$$ time:

```c
void make_heap(priority_queue *q, item_type s[], int n) {
Expand Down

0 comments on commit c3738ba

Please sign in to comment.