0% found this document useful (0 votes)
17 views

Heap Sort

Uploaded by

reisenudongein00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Heap Sort

Uploaded by

reisenudongein00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Heap Sort

Heapsort
6 10 1 4 7 9 3 2 8 11

Build Heap (Max)


Heap Sort Algorith

11 10 9 8 7 1 3 2 4 6
(build + sort)

Sort Max Heap

1 2 3 4 6 7 8 9 10 11
m

Heapsort Algorithm
Function Heapsort(A)
1 #Create max heap
Build_Max_Heap from unordered array A
2 # Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
discard node i from heap (decrement heap size)
sift(A[1:i-1], 1) because new root may violate max heap property
Build Max Heap
Function Build_Max_Heap(A
set heap size to the length of the arra
for j= n/2 down to 1 do
sift(A, j)

20,8,7,9,6,54
20,8,54,9,6,7
)

Heap
■ The root of the tree is A[1], and given the index i of a node, we can
easily compute the indices of its parent, left child, and right child:

function parent(i
return i/2

function left(i
return 2*i

function right(i
return 2 *i + 1

Max-Heapify (sift)
function sift(arr,i
n len(arr 20,8,7,9,6,54
l left(i
r right(i i=3, l=6, r=7, n=6

if l <= n and arr[l] > arr[i] the largest=l=6


largest
else
largest 20,8,54,9,6,7
if r <= n and arr[r] > arr[largest] the
largest

if largest != i the
arr[i] arr[largest]
sift(arr, largest
return arr

Start with an array (it is not a max heap)


6

1
10

4 7 9 3

2 8 11

6 10 1 4 7 9 3 2 8 11
Exchange 7 and 11 function sift(arr,i
n
l
len(arr
left(i
r right(i
Function Build_Max_Heap(A
set heap size to the length of the arra if l <= n and arr[l] > arr[i] the
largest
for j= n/2 down to 1 do
sift(A, j)
6 else
largest

if r <= n and arr[r] > arr[largest] then


largest =
function parent(i 1
return i/2
10 if largest != I the
arr[i] arr[largest
function left(i sift(arr,largest
return 2*i return arr

function right(i
4 7 9 3
return 2 *i + 1

2 8 11

6 10 1 4 7 9 3 2 8 11
j

Exchange 4 and 8
Function Build_Max_Heap(A
set heap size to the length of the arra
for j= n/2 down to 1 do
sift(A, j)
6

1
10

4 11 9 3

2 8 7

6 10 1 4 11 9 3 2 8 7
j
)

Exchange 9 and 1
Function Build_Max_Heap(A
set heap size to the length of the arra
for j= n/2 down to 1 do
sift(A, j)
6

1
10

8 11 9 3

2 4 7

6 10 1 8 11 9 3 2 4 7
j
)

Exchange 10 and 11
Function Build_Max_Heap(A
set heap size to the length of the arra
for j= n/2 down to 1 do
sift(A, j)
6

9
10

8 11 1 3

2 4 7

6 10 9 8 11 1 3 2 4 7
j
)

Exchange 6 and 11 function sift(arr,i


n
l
len(arr)
left(i
r right(i
Function Build_Max_Heap(A
set heap size to the length of the arra if l <= n and arr[l] > arr[i] the
largest
for j= n/2 down to 1 do
sift(A, j)
6 else
largest

if r <= n and arr[r] > arr[largest] then


largest
9
11 if largest != i the
arr[i] arr[largest
sift(arr,largest
return arr
8 10 1 3

2 4 7

6 11 9 8 10 1 3 2 4 7
j

Exchange 6 and 10 function sift(arr,i


n
l
len(arr
left(i
r right(i
Function Build_Max_Heap(A
set heap size to the length of the arra if l <= n and arr[l] > arr[i] the
largest
for j= n/2 down to 1 do
sift(A, j)
11 else
largest

if r <= n and arr[r] > arr[largest] then


largest
9
6 if largest != i the
arr[i] arr[largest
sift(arr,largest
return arr
8 10 1 3

2 4 7

11 6 9 8 10 1 3 2 4 7
j i largest

Exchange 6 and 7 function sift(arr,i


n
l
len(arr)
left(i
r right(i
Function Build_Max_Heap(A
set heap size to the length of the arra if l <= n and arr[l] > arr[i] the
largest
for j= n/2 down to 1 do
sift(A, j)
11 else
largest

if r <= n and arr[r] > arr[largest] then


largest
9
10 if largest != i the
arr[i] arr[largest
sift(arr,largest
return arr
8 6 1 3

2 4 7

11 10 9 8 6 1 3 2 4 7
j i largest

max_heapify function sift(arr,i


n
l
len(arr
left(i
r right(i
Function Build_Max_Heap(A
set heap size to the length of the arra if l <= n and arr[l] > arr[i] the
largest
for j= n/2 down to 1 do
sift(A, j)
11 else
largest

if r <= n and arr[r] > arr[largest] then


largest
9
10 if largest != i the
arr[i] arr[largest
sift(arr,largest
return arr
8 7 1 3

2 4 6

11 10 9 8 7 1 3 2 4 6
j i largest

Sorting

Function Heapsort(A)
#Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap property
Function Heapsort(A)
Exchange 11 and 6 #Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
11 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
10 9

8 7 1 3

2 4 6

11 10 9 8 7 1 3 2 4 6
Remove 11 from the heap

10 9

8 7 1 3

2 4 11

6 10 9 8 7 1 3 2 4 11
Swap 6 and 10 function sift(arr,i
n
l
len(arr
left(i
r right(i

if l <= n and arr[l] > arr[i] the


largest
else
6 largest

if r <= n and arr[r] > arr[largest] then


largest

10 9
if largest != i the
arr[i] arr[largest
sift(arr,largest
return arr
8 7 1 3

2 4 11

6 10 9 8 7 1 3 2 4 11

Exchange 6 and 8

10

6 9

8 7 1 3

2 4 11

10 6 9 8 7 1 3 2 4 11
Function Heapsort(A)
Exchange 10 and 4 #Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
10 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
8 9

6 7 1 3

2 4 11

10 8 9 6 7 1 3 2 4 11
Remove 10 from the heap

8 9

6 7 1 3

2 10 11

4 8 9 6 7 1 3 2 10 11
Exchange 4 and 9

8 9

6 7 1 3

2 10 11

4 8 9 6 7 1 3 2 10 11
Function Heapsort(A)
Exchange 9 and 2 #Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
9 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
8 4

6 7 1 3

2 10 11

9 8 4 6 7 1 3 2 10 11
Remove 9 from the heap

8 4

6 7 1 3

9 10 11

2 8 4 6 7 1 3 9 10 11
Exchange 2 and 8

8 4

6 7 1 3

9 10 11

2 8 4 6 7 1 3 9 10 11
Exchange 2 and 7

2 4

6 7 1 3

9 10 11

8 2 4 6 7 1 3 9 10 11
Function Heapsort(A)
Exchange 8 and 3 #Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
8 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
7 4

6 2 1 3

9 10 11

8 7 4 6 2 1 3 9 10 11
Remove 8 from the heap

7 4

6 2 1 8

9 10 11

3 7 4 6 2 1 8 9 10 11
Exchange 3 and 7

7 4

6 2 1 8

9 10 11

3 7 4 6 2 1 8 9 10 11
Exchange 3 and 6

3 4

6 2 1 8

9 10 11

7 3 4 6 2 1 8 9 10 11
Function Heapsort(A)
Exchange 1 and 7 #Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
7 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
6 4

3 2 1 8

9 10 11

7 6 4 3 2 1 8 9 10 11
Remove 7 from the heap

6 4

3 2 7 8

9 10 11

1 6 4 3 2 7 8 9 10 11
Exchange 1 and 6

6 4

3 2 7 8

9 10 11

1 6 4 3 2 7 8 9 10 11
Exchange 1 and 3

1 4

3 2 7 8

9 10 11

6 1 4 3 2 7 8 9 10 11
Function Heapsort(A)
Exchange 6 and 2 and #Create
remove max heap from the heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
6 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
3 4

1 2 7 8

9 10 11

6 3 4 1 2 7 8 9 10 11
Exchange 4 and 2

3 4

1 6 7 8

9 10 11

2 3 4 1 6 7 8 9 10 11
Function Heapsort(A)
Exchange 4 and 1 #Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
4 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
3 2

1 6 7 8

9 10 11

4 3 2 1 6 7 8 9 10 11
Remove 4, exchange 1 and 3

3 2

4 6 7 8

9 10 11

1 3 2 4 6 7 8 9 10 11
Function Heapsort(A)
Exchange 2 and 3, and#Create
remove
max heap 3 from heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
3 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
1 2

4 6 7 8

9 10 11

3 1 2 4 6 7 8 9 10 11
Function Heapsort(A)
Exchange 1 and 2 and remove from heap #Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
2 discard node i from heap (decrement heap size)
sift(A[1:i-1],1) because new root may violate max heap
property
1 3

4 6 7 8

9 10 11

2 1 3 4 6 7 8 9 10 11
The array is sorted

2 3

4 6 7 8

9 10 11

1 2 3 4 6 7 8 9 10 11
Sorted Output
6 10 1 4 7 9 3 2 8 11

Build Heap (Max)


Heap Sort Algorith

11 10 9 8 7 1 3 2 4 6
(build + sort)

Sort Max Heap

1 2 3 4 6 7 8 9 10 11
m

Heapsort Algorithm
Function Heapsort(A)
#Create max heap
Build_Max_Heap from unordered array A
# Finish sorting
for i = n downto 2 do
exchange A[1] with A[i]
discard node i from heap (decrement heap size)
sift(A[1:i-1], 1) because new root may violate max heap property
Build Max Heap
Function Build_Max_Heap(A
set heap size to the length of the arra
for j= n/2 down to 1 do
sift(A, j)

Heap
■ The root of the tree is A[1], and given the index i of a node, we can
easily compute the indices of its parent, left child, and right child:

function parent(i
return i/2

function left(i
return 2*i

function right(i
return 2 *i + 1

Max-Heapify (sift)
function sift(arr,i
n len(arr
l left(i
r right(i

if l <= n and arr[l] > arr[i] the


largest
else
largest

if r <= n and arr[r] > arr[largest] the


largest

if largest != i the
arr[i] arr[largest]
sift(arr, largest
return arr

You might also like