1234567891011121314151617181920212223242526272829303132333435363738 |
- ''' Luis Andrés López Mañán
- Program written by hand as a draft on 09/19/2022
- Credit goes to GeeksForGeeks
- Link: https://www.geeksforgeeks.org/python-program-for-heap-sort/
- Last access on 09/19/2022
- Program wrriten and edited on 10/10/2022
- '''
-
- # This function heapifies a subtree rooted at an index
- # i. Also, n is the size of a heap and arr is array.
-
- def heapify(lista, n, i):
- # largest is root for now
- largest = i
-
- # left child of root
- l = 2 * i + 1
-
- # right child of root
- r = 2 * i + 2
-
- # Checks if root has a left child and is greater
- # than root
- if l < n and lista[i] < lista[l] :
- largest = l
-
- # Checks if root has a right child and is greater
- # than root
- if r < n and lista[largest] < lista[r]:
- largest = r
-
- # If necessary, this changes root by swapping va-
- # lues
- if largest != i:
- lista[i], lista[largest] = lista[largest], lista[i]
-
- # This heapifies the root repeatedly
- heapify(lista, n, largest)
|