Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap...

46
Heap Sort

Transcript of Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap...

Page 1: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Heap Sort

Page 2: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Heapsort AlgorithmHeapsort(A):

#Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size)

Max-heapify(A, 1) because new root may violate max heap property

Page 3: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Build Max HeapBuild_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

Page 4: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

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: def parent(i):

return i/2 def left(i): try: return 2*i except: pass def right(i): try: return 2 *i + 1 except: pass

Page 5: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Max-Heapifydef max_heapify(arr,i):

n = len(arr) l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr

Page 6: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

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

6

10 1

4 7

2 8 11

9 3

6 10 1 4 7 9 3 2 8 11

Page 7: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 7 and 11

6

10 1

4 7 9 3

2 8 11

6 10 1 4 7 9 3 2 8 11j

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

def max_heapify(arr,i): n = len(arr)

l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr

def parent(i): return i/2 def left(i): try: return 2*i except: pass def right(i): try: return 2 *i + 1 except: pass

Page 8: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 4 and 8

6

10 1

4 11 9 3

2 8 7

6 10 1 4 11 9 3 2 8 7j

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

Page 9: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 9 and 1

6

10 1

8 11 9 3

2 4 7

6 10 1 8 11 9 3 2 4 7

j

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

Page 10: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 10 and 11

6

10 9

8 11 1 3

2 4 7

6 10 9 8 11 1 3 2 4 7

j

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

Page 11: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 6 and 11

6

11 9

8 10 1 3

2 4 7

6 11 9 8 10 1 3 2 4 7

j

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

def max_heapify(arr,i): n = len(arr)

l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr

Page 12: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 6 and 10

11

6 9

8 10 1 3

2 4 7

11 6 9 8 10 1 3 2 4 7

j

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

i largest

def max_heapify(arr,i): n = len(arr)

l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr

Page 13: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 6 and 7

11

10 9

8 6 1 3

2 4 7

11 10 9 8 6 1 3 2 4 7

j i

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

largest

def max_heapify(arr,i): n = len(arr)

l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr

Page 14: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

max_heapify

11

10 9

8 7 1 3

2 4 6

11 10 9 8 7 1 3 2 4 6

j i

Build_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

largest

def max_heapify(arr,i): n = len(arr)

l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr

Page 15: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Sorting

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size)

Max-heapify(A,1) because new root may violate max heap property

Page 16: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 11 and 6

11

10 9

8 7 1 3

2 4 6

11 10 9 8 7 1 3 2 4 6

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 17: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Remove 11 from the heap

6

10 9

8 7 1 3

2 4 11

6 10 9 8 7 1 3 2 4 11

Page 18: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Swap 6 and 10

6

10 9

8 7 1 3

2 4 1111

6 10 9 8 7 1 3 2 4 11

Page 19: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 6 and 8

10

6 9

8 7 1 3

2 4 1111

10 6 9 8 7 1 3 2 4 11

Page 20: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 10 and 4

10

8 9

6 7 1 3

2 4 1111

10 8 9 6 7 1 3 2 4 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 21: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Remove 10 from the heap

4

8 9

6 7 1 3

2 10 1111

4 8 9 6 7 1 3 2 10 11

Page 22: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 4 and 9

4

8 9

6 7 1 3

2 1110

4 8 9 6 7 1 3 2 10 11

Page 23: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 9 and 2

9

8 4

6 7 1 3

2 1110

9 8 4 6 7 1 3 2 10 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 24: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Remove 9 from the heap

2

8 4

6 7 1 3

9 1110

2 8 4 6 7 1 3 9 10 11

Page 25: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 2 and 8

2

8 4

6 7 1 3

11109

2 8 4 6 7 1 3 9 10 11

Page 26: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 2 and 7

8

2 4

6 7 1 3

11109

8 2 4 6 7 1 3 9 10 11

Page 27: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 8 and 3

8

7 4

6 2 1 3

11109

8 7 4 6 2 1 3 9 10 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 28: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Remove 8 from the heap

3

7 4

6 2 1 8

11109

3 7 4 6 2 1 8 9 10 11

Page 29: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 3 and 7

3

7 4

6 2 1

11109

8

3 7 4 6 2 1 8 9 10 11

Page 30: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 3 and 6

7

3 4

6 2 1

11109

8

7 3 4 6 2 1 8 9 10 11

Page 31: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 1 and 7

7

6 4

3 2 1

11109

8

7 6 4 3 2 1 8 9 10 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 32: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Remove 7 from the heap

1

6 4

3 2 7

11109

8

1 6 4 3 2 7 8 9 10 11

Page 33: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 1 and 6

1

6 4

3 2

11109

87

1 6 4 3 2 7 8 9 10 11

Page 34: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 1 and 3

6

1 4

3 2

11109

87

6 1 4 3 2 7 8 9 10 11

Page 35: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 6 and 2 and remove from the heap

6

3 4

1 2

11109

87

6 3 4 1 2 7 8 9 10 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 36: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 4 and 2

2

3 4

1

11109

876

2 3 4 1 6 7 8 9 10 11

Page 37: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 4 and 1

4

3 2

1

11109

876

4 3 2 1 6 7 8 9 10 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 38: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Remove 4, exchange 1 and 3

1

3 2

4

11109

876

1 3 2 4 6 7 8 9 10 11

Page 39: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 2 and 3, and remove 3 from heap

3

1 2

11109

8764

3 1 2 4 6 7 8 9 10 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 40: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Exchange 1 and 2 and remove from heap

2

1

11109

8764

3

2 1 3 4 6 7 8 9 10 11

Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size) Max-heapify(A, 1) because new root may violate max heap property

Page 41: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

The array is sorted

1

3

11109

8764

2

1 2 3 4 6 7 8 9 10 11

Page 42: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Sorted Output6 10 1 4 7 9 3 2 8 11

Hea

p So

rt Al

gorit

hm (b

uild

+ s

ort)

1 2 3 4 6 7 8 9 10 11

11 10 9 8 7 1 3 2 4 6

Build Heap (Max)

Sort Max Heap

Page 43: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Heapsort AlgorithmHeapsort(A):

#Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto 2

exchange A[1] with A[i] discard node i from heap (decrement heap size)

Max-heapify(A, 1) because new root may violate max heap property

Page 44: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Build Max HeapBuild_Max_Heap(A): set heap size to the length of the array iterate j from [A.length/2] down to 1: Max-heapify(A, j)

Page 45: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

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: def parent(i):

return i/2 def left(i): try: return 2*i except: pass def right(i): try: return 2 *i + 1 except: pass

Page 46: Heap Sort - University Of MarylandHeap Sort. Heapsort Algorithm Heapsort(A): #Create max heap Build_Max_Heap from unordered array A # Finish sorting iterate i from A.length downto

Max-Heapifydef max_heapify(arr,i):

n = len(arr) l = left(i) r = right(i) if l <= n and arr[l] > arr[i]: largest = l else: largest = i if r <= n and arr[r] > arr[largest]: largest = r if largest != i: temp = arr[i] arr[i] = arr[largest] arr[largest] = temp max_heapify(arr,largest) return arr