The Python heap sorting algorithm in less than 128 words

The principle of the heap sorting algorithm is as follows:

  • We search for the father of the last knot of the tree and we sift it: the father of a knot in a binary tree corresponds to the lower round of (i-1)/2 with i the position of the knot in the table that stores the tree.
  • Sieving a knot involves visiting his right and left son and swapping the root with the larger of the two.
  • The tree is sifted until it reaches the root, the largest number of the tree is then in the root position at the beginning of the painting.
  • The root that is the largest number is swapped with the last leaf (i.e. the n position of the table that stores the n-size tree). This number is considered to be well ordered (i.e. it is in last position and it is the largest)
  • The sieving and swapping operation is repeated by considering the subtree that goes from root to n-1 (so as not to move the number that has been correctly ordered) until we reach the left wire of the root (index 1)

See wikipedia link: https://fr.wikipedia.org/wiki/Tri_par_tas

from math import
#128mots.com
def indice_fils_gauche (noeud_pere:int):
	return (noeud_pere - 2) - 1

def indice_fils_droit (noeud_pere:int):
	return (noeud_pere - 2) - 2
#Retourne the pere node index if it exists (node must be 0)
def indice_noeud_pere(knot):
	#Si it is the lower rounding of (knot-1)/2
	return floor (knot - 1)/2)

#permute 2 elements of a table and incrementally the overall variable compteur_permutation to be initialized in the hand
def permute (table, index1: int, index2:int):
	print ("Permutation of the element" - str (index1) - "with element" - str (index2))
	print(" Before permutation: " - str (table))
	globalcompteur_permutation
	if (index1!2):
	#on back up the value of Index 1 in a temporary variable
		tmp - table[indice1]
		table[indice1] - table[indice2]
		table[indice2] - tmp
		compteur_permutation 1
	print(" After permutation: " - str (table))

#Tamise a binary tree from a parametre knot
#1. the knot is compared with the right and left wire (if they exist)
#2. we swap with the highest value
#128mots.com
def thames (tree, knot, length):
	#On visits right and left son
	print ("Tamisage of" - str (knot) - " tree: " - str (tree)	
	indexSonRight - indice_fils_droit (knot)
	IndexLeft - indice_fils_gauche (knot)
	print ("IndexSonRight" - str (indexSon))
	print ("Left-hand index"" - str (left-hand index)


	if (indexSonRight< longueur): # si le fils droit existe longueur):="" #="" si="" le="" fils="" droit=""></ longueur): # si le fils droit existe>
		if (tre[indiceFilsDroit]e- tree[indiceFilsGauche]): #Si the right son is larger than the left wire
			if (tre[indiceFilsDroit]e- tree[noeud]): #Si the right wire is larger than the sifted knot
				permute (tree, indexSonright,knot)
		elif (tre[indiceFilsGauche]e - tre[noeud]e): #Si the left wire is higher than the sifted knot, so you swap
			permute (tree, left-hand index, knot)
	elif (Left-hand index< longueur): #Si le fils gauche existe longueur):="" #si="" le="" fils="" gauche=""></ longueur): #Si le fils gauche existe>
		if (tre[indiceFilsGauche]e - tre[noeud]e): #Si the left wire is higher than the sifted knot so we swap
			permute (tree, left-hand index, knot)
	print(" After sieving: " - str (tree))			
#128mots.com

compteur_permutation 0
tree - #[11, 222, 3, 24, 5, 46, 67, 899]On writes a tree in the form of a painting
print ("starting tree: " - str (tree)

#128mots.com
#Tamisage initial
#on takes the last element of the arbe which is a leaf and one looks for his father
IndexDuNoeudPere - indice_noeud_pere (tree)-1) 
#on sifting is compared to the father knot with the right and left son
#puis you swap with the highest value
for i in range (DuNoeudPere index,-1,-1): #On siftto to the root
	thames (tree,i,len (tree)

permute (tree, len (tree)-1.0) #on swaps the first element and the last 
#suite sieving is the greatest value so it is placed at the end of the table

#on repeats the sieving
for i in range(len(tree)-1,1,-1): 
	indexDuNoeudPereDeI - indice_noeud_pere (i-1) #on takes the element i of the arbe which is a sheet and one looks for his father
	for j in range (duKnotPereDeI index,-1,-1): #On siftto to the root
		thames (tree, j,i)
	permute (tree, i-1.0)

print ("final result of sorting: " - str (tree))
print ("number of permutations: " - str(compteur_permutation))