Heapsort Flashcards

(20 cards)

1
Q

What is a heap?

A

A complete binary tree where every node is greater than or equal to its children (max-heap).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a complete binary tree?

A

A binary tree where all levels are fully filled except possibly the last, which is filled from left to right with no gaps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the heap property in a max-heap?

A

Every parent node is greater than or equal to its children.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the time complexity of getMax in a heap?

A

Θ(1) — the maximum is always at the root.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the time complexity of insert and deleteMax in a heap?

A

Θ(log n), due to sifting up or down.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the sift-down operation used for?

A

Restoring the heap after deleting the max by pushing the root down to its correct position.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the sift-up operation used for?

A

Restoring the heap after inserting a new element by bubbling it up to its correct position.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why must a heap be a complete tree?

A

To allow efficient storage in an array without gaps and predictable child/parent indices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What array indices correspond to a node’s children?

A

Left child = 2n + 1, right child = 2n + 2 (0-based indexing).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What index gives the parent of a node in a heap array?

A

Parent = floor((n - 1) / 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is heapify?

A

The process of building a heap from an unsorted array using bottom-up sifting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the time complexity of heapify?

A

Θ(n), due to the distribution of work among tree levels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Heapsort?

A

A sorting algorithm that uses a heap to repeatedly extract the max and place it at the end of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the time complexity of Heapsort?

A

Θ(n log n), and it uses no extra memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does Heapsort relate to selection sort?

A

It repeatedly selects the maximum, but finds it efficiently using a heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Is Heapsort stable?

A

No, equal elements may not preserve their input order.

17
Q

Why is the completeness of the tree crucial for array-based heaps?

A

Because it ensures all positions can be calculated without gaps, enabling efficient storage.

18
Q

What operation allows priority adjustment in a heap?

A

increaseKey — it increases the priority and sifts the item to its correct position.

19
Q

Why is a hashtable useful for implementing increaseKey?

A

It helps track the current index of each item in the heap for quick access.

20
Q

What real-world problem can heaps solve efficiently?

A

Implementing priority queues, such as in scheduling or Dijkstra’s algorithm.