Sorting by selection in Python

tab[111, 34, 22, 55, 4, 2, 1, 77]

for i in range (0,len(tab)-1):
	min i
	for j in range (i-1,len(tab):c)
		if tab<tab[j]:></tab[min]:>
			min - J
	if (min!
		tmp - tab[i]
		ta[i]b-tab[min]
		tab[min] - tmp

print (tab)

If we consider the "if tab" comparison operation<tab" et n la taille du tableau. et="" n="[j]" la=""[min] taille="" du=""></tab" et n la taille du tableau.>

If i -0 -0 (n-1) comparisons

If i – 1 – (n-2) comparisons

… If i 'n-2' – 1 comparison

or n – (n-1) comparisons

So the loop for i in range (0,len(tab)-1): runs n-1 times

The for j in range loop (i-1,len(tab):runs (n-(i-1) – 1) times

The complexity in the number of comparisons is equal to the sum of the following n-1 terms (i – 1, … i – n-1)

C – (n-2) -1 – (n-3) -1…..-1-0 -1-0 -(n-1)–(n-2)-…-1 -n.(n-1)/2 (this is the sum of the entire first n-1).

The complexity in the number of comparisons is in the order of n2, one writes O(n2).

Auteur / autrice

Retour en haut