Add more algorithms

This commit is contained in:
Vinicius Silva 2024-05-13 19:22:31 -03:00
parent e32352b117
commit f15cbebb9d
2 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,21 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def swap(i , j):
tmp = lista[i]
lista[i] = lista[j]
lista[j] = tmp
def insertion():
for i in range(0, len(lista)-1):
j = (i+1)
while j > 0 and lista[j-1] < lista[j]:
swap(j-1, j)
j -= 1
if __name__ == '__main__':
insertion()
print(lista)

View File

@ -0,0 +1,24 @@
lista = [0,1,5,3,15,16,9,10,4,3,30,5,20,48,71,82]
def swap(i , j):
tmp = lista[j]
lista[j] = lista[i]
lista[i] = tmp
def selection():
for i in range(0, len(lista)):
pos = i
for j in range((i+1), len(lista)):
if lista[j] > lista[pos]:
pos = j
swap(pos, i)
if __name__ == '__main__':
selection()
print(lista)