Python insertion sorting in less than 128 words

Insertion sorting is a slow, stable sorting, in place (we work on the structure directly and not a copy).

The sorting by insertion is comparable to the sorting that one performs of a deck of card. 

def tri_par_insertion (table):
	#parcours of all the elements of the painting
	globalnombre_operation
	for i in range (1,len(tab)-1):
		#pour each of the elements of the table we go through the precedents and exchange
		#on back up the value of the current element
		ValueElementCurrent - table[i]
		print ("common element: " ' ' ' ' '[i] ' - index i' ' ' str(i))
		j i
		#on defines the j index and we go through the predents elements as long as it'
		while(j-0 and table[j-1]-valueElementCourant):
			#si the element traveled among the previous values is higher than the current element it is inserted in its place and one decal sades on the left
			table[j] - table[j-1]
			J - 1
			nombre_operation '1'
		#lorsqu we come out of the loop we place all the elements higher to the current element to the right of j then we must put the current element to the position j
						
	table [j]- valueElementCourant
		
nombre_operation 0
tab[3,9,6,1,2,4,7,5,8]
print ("before sorting by insertion: " - str(tab))
tri_par_insertion (tab)
print ("after sorting by insertion: " - str(tab))
print ("number of operations:" - str(nombre_operation))

Auteur / autrice

Retour en haut