05b-HeapSorting
-
Upload
frankjamison -
Category
Documents
-
view
226 -
download
0
Transcript of 05b-HeapSorting
-
8/13/2019 05b-HeapSorting
1/18
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 1
Heap
Sorting
-
8/13/2019 05b-HeapSorting
2/18
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 2
Heap Sort
Heap sort uses a heap as described in the earlier lectures
As we said before, a heap is a binary tree with the following two
properties:
Value of each node is not less than the values stored in each
of its children
The tree is perfectly balanced and the leaves in the level are
all in the leftmost positions
-
8/13/2019 05b-HeapSorting
3/18
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 3
The procedure is:
The data are transformed into a heap first
Doing this, the data are not necessarily sorted; however, we
know that the largest element is at the root
Thus, start with a heap tree,
Swap the root with the last element
Restore all elements except the last element into a heap
again
Repeat the process for all elements until you are done
-
8/13/2019 05b-HeapSorting
4/18
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 4
template
void HeapSort(T data[ ], int size){
for (int i = (size/2)-1; i>=0; i--)
MoveDown(data, i, size-1); // creates the heap
for (i=size-1; i>=1; --i)
{
Swap (data[0], data[i]); // move the largest item to data[i]
MoveDown(data, 0, i-1); // restores the heap
}
}
Algorithm and Code for Heap sort
HeapSort(data[ ] ,n)
transform data into a heap
for (i=n-1; i>1; i --)
swap the root with the element in position i ;
restore the heap property for the tree data[0] data[i-1]
-
8/13/2019 05b-HeapSorting
5/18
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 5
Example of Heap Sort
We first transform thedata into heap
2
8
6
1
10
15
3
12
11
2
101
68
1112
315
The initial tree is formedas follows
-
8/13/2019 05b-HeapSorting
6/18
Dr. Ahmad R. Hadaegh
A.R. Hadaegh Nati onal University Page 6
2
1012
68
111
315
We turn the array into a heap first
2
101
68
1112
315
2
1012
158
111
36
2
1012
68
111
315
-
8/13/2019 05b-HeapSorting
7/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 7
2
108
1512
111
36
2
1012
158
111
36
2
1011
1512
81
36
2
108
1512
111
36
-
8/13/2019 05b-HeapSorting
8/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 8
15
1011
612
81
32
15
1011
212
81
36
15
1011
212
81
36
2
1011
1512
81
36
-
8/13/2019 05b-HeapSorting
9/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 9
Now we start to sort the elements
15
1011
612
81
32
8
1011
612
151
32
12
108
611
151
32
Swap the root
with the last
element
Restore the heap
-
8/13/2019 05b-HeapSorting
10/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 10
Swap the root
with the last
element
Restore the heap
12
108
611
151
32
1
108
611
1512
32
11
18
610
1512
32
-
8/13/2019 05b-HeapSorting
11/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 11
Swap the root
with the last
element
Restore the heap
3
18
610
1512
112
10
13
68
1512
112
11
18
610
1512
32
-
8/13/2019 05b-HeapSorting
12/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 12
Swap the root
with the last
element
Restore the heap
8
12
63
1512
1110
10
13
68
1512
112
2
13
68
1512
1110
-
8/13/2019 05b-HeapSorting
13/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 13
Swap the root
with the last
element
Restore the heap
8
12
63
1512
1110
1
82
63
1512
1110
6
82
13
1512
1110
-
8/13/2019 05b-HeapSorting
14/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 14
Swap the root
with the last
element
Restore the heap
6
82
13
1512
1110
2
86
13
1512
1110
3
86
12
1512
1110
-
8/13/2019 05b-HeapSorting
15/18
-
8/13/2019 05b-HeapSorting
16/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 16
Swap the root
with the last
element
Restore the heap
2
86
31
1512
1110
1
86
32
1512
1110
1
86
32
1512
1110
-
8/13/2019 05b-HeapSorting
17/18Dr. Ahmad R. HadaeghA.R. Hadaegh Nati onal University Page 17
Place the elements into array using
breadth first traversal
1
86
32
1512
1110
1
2
3
6
8
10
11
12
15
-
8/13/2019 05b-HeapSorting
18/18D Ah d R H d hA R Hadaegh Nati onal Un iversity Page 18
Complexity of heap sort
The heap sort requires a lot of movement which can be inefficientfor large objects
In the second phase when we start to sort the elements whilekeeping the heap, we exchange n-1times the root with theelement in position iand also restore the heap n-1times whichtakes O(nlogn)
In general:
The first phase, where we turn the array into heap, requiresO(n)steps
And the second phase when we start to sort the elementsrequires
O(n-1)swap + O(nlogn)operations to restore the heap
Total = O(n) + O(nlogn) + O(n-1) = O(nlogn)