CS 261
–
Data Structures
Build?Heap and Heap Sort
Heap Implementation:
Constructors
void buildHeap(struct dyArray * data) { int I;
int max = dyArraySize(data);
// All nodes greater than max/2

1 are leaves and thus adhere to the heap property!
for (i = max / 2

1; i >= 0; i

)
adjustHeap(data, max, i);
// Make subtree rooted at
i
a heap.
}
•
At the beginning, only the leaves are proper heaps:
–
Leaves are all nodes with indices greater than max / 2
•
At each step, the subtree rooted at index
i
becomes
a heap
Heap Implementation:
Build heap
void buildHeap(struct vecctor * data) { int I;
int max = vectorSize(data);
// All nodes greater than max/2

1 are leaves and thus adhere to the heap property!
for (i = max / 2

1; i >= 0; i

)
adjustHeap(data, max, i);
// Make subtree rooted at
i
a heap.
}
•
For all subtrees that are are not already heaps
(initially, all
inner, or non

leaf, nodes)
:
–
Call
adjustHeap
with the
largest
node index that is not already
guaranteed to be a heap
–
Iterate until the root node becomes a heap
•
Why call
adjustHeap
with the
largest
non

heap node?
–
Because its children, having larger indices, are already
guaranteed to be heaps
Heap Implementation:
adjustHeap
12
5
4
7
7
8
3
9
11
10
2
1
7
2
5
3
8
4
3
0
12
11
max
5
7
6
4
7
9
8
11
9
10
10
2
i
(
max/2

1
)
Already heaps
(leaf nodes)
adjustHeap
First iteration:
adjust largest non

leaf
node (index 4)
Heap Implementation:
adjustHeap (cont.)
12
5
4
7
7
8
2
9
11
10
3
1
7
2
5
3
8
4
2
0
12
11
max
5
7
6
4
7
9
8
11
9
10
10
3
i
(no adjustment needed)
Already heaps
adjustHeap
Second iteration:
adjust largest non

heap
node (index 3)
Heap Implementation:
adjustHeap (cont.)
12
5
4
7
7
8
2
9
11
10
3
1
7
2
5
3
8
4
2
0
12
11
max
5
7
6
4
7
9
8
11
9
10
10
3
i
Already heaps
adjustHeap
Third iteration:
adjust largest non

heap
node (index 2)
Heap Implementation:
adjustHeap (cont.)
12
4
5
7
7
8
2
9
11
10
3
1
7
2
4
3
8
4
2
0
12
11
max
5
7
6
5
7
9
8
11
9
10
10
3
i
Already heaps
adjustHeap
Fourth iteration:
adjust largest non

heap
node (index 1)
Heap Implementation:
adjustHeap (cont.)
12
4
5
2
7
8
3
9
11
10
7
1
2
2
4
3
8
4
3
0
12
11
max
5
7
6
5
7
9
8
11
9
10
10
7
i
Already heaps
Fifth iteration:
adjust largest non

heap
node (index 0
root
)
adjustHeap
Heap Implementation:
adjustHeap (cont.)
2
4
5
3
7
8
7
9
11
10
12
1
3
2
4
3
8
4
7
0
2
5
7
6
5
7
9
8
11
9
10
10
12
Already heaps
(entire tree)
Heap Sort

Basic idea
•
Build the initial heap
•
Repeately
–
Remove the smallest element (root)
–
Rebuild the heap
So the heap starts out with n elements, then has n

1, then n

2, and
so on
Where should you store the elements that are removed?
Why not put them in the other end of the array? (The one that is no
longer being used for the heap).
Heap Implementation:
sort
void heapSort(struct dyArray * data) { int i;
buildHeap(data);
// Build initial heap.
for (i = dyArraySize(data)
–
1; i > 0; i

) {
// For each of the n elements:
dyArraySwap(data,0, i);
// Swap last element with the first (smellest) element
adjustHeap(data, i, 0);
// Rebuild heap property.
}
}
Sorts the data in descending order
(from largest to smallest)
:
–
Builds heap from initial
(unsorted)
data
–
Iteratively swaps the smallest element
(at index 0)
with last
unsorted
element
–
Adjust the heap after each swap, but only considers the
unsorted
data
View from Middle of Execution
Heap Analysis:
sort
•
Execution time:
–
Build heap:
n
calls to
adjustHeap
=
n
log
n
–
Loop:
n
calls to
adjustHeap
=
n
log
n
–
Total: 2
n
log
n
= O(
n
log
n
)
•
Advantages/disadvantages
:
–
Same average as merge sort and quick sort
–
Doesn’t require extra space as the merge sort does
–
Doesn’t suffer if data is already sorted or mostly sorted
On the worksheet
•
I’m having you represent the heap as a tree (easier to
visualize than the array representation)
•
Build the initial heap
•
Then repeately remove the smallest element, then build
the heap again, until there are no elements in the tree.
Enter the password to open this PDF file:
File name:

File size:

Title:

Author:

Subject:

Keywords:

Creation Date:

Modification Date:

Creator:

PDF Producer:

PDF Version:

Page Count:

Preparing document for printing…
0%
Comments 0
Log in to post a comment