Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Algoritimos Buble sort em python, Esquemas de Estruturas de Dados e Algoritmos

Contém as tres formas do algoritimo buble sort

Tipologia: Esquemas

2024

À venda por 20/03/2024

vagner-vc-7
vagner-vc-7 🇧🇷

17 documentos

1 / 2

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
import random
def prenche_vetor():
vetor = []
for i in range(0, 5):
valor = random.randint(10, 50)
vetor.append(valor)
return vetor
def bublesort():
vetor = []
#Esse for gera valores randomicos para o vetor
for i in range(0, 5):
valor = random.randint(10, 50)
vetor.append(valor)
print(vetor)
# Esse for faz a ordenação
for i in range(0, len(vetor)):
for j in range(0, len(vetor) - 1):
if vetor[j] > vetor[j + 1]:
aux = vetor[j]
vetor[j] = vetor[j + 1]
vetor[j + 1] = aux
print(vetor)
def bublesort_melhorado1():
array = prenche_vetor()
print(array)
for i in range(0, len(array) -1):
for j in range(len(array) - 1, i, -1):
if array[j] < array[j - 1]:
aux = array[j]
array[j] = array[j - 1]
array[j - 1] = aux
print(array)
def bublesort_melhorado2():
# Aqui oque fazemos é prdenar de forma decrescente
array = prenche_vetor()
print(array)
n = 1
troca = 1
while(n<=5 and troca == 1):
troca = 0
for i in range(0, len(array) -1):
if array[i] < array[i + 1]:
troca = 1
aux = array[i]
array[i] = array[i + 1]
array[i + 1] = aux
n = n + 1
print(array)
def main():
print('Algoritimo BubleSort')
pf2

Pré-visualização parcial do texto

Baixe Algoritimos Buble sort em python e outras Esquemas em PDF para Estruturas de Dados e Algoritmos, somente na Docsity!

import random def prenche_vetor(): vetor = [] for i in range( 0 , 5 ): valor = random.randint( 10 , 50 ) vetor.append(valor) return vetor def bublesort(): vetor = [] #Esse for gera valores randomicos para o vetor for i in range( 0 , 5 ): valor = random.randint( 10 , 50 ) vetor.append(valor) print(vetor)

Esse for faz a ordenação

for i in range( 0 , len(vetor)): for j in range( 0 , len(vetor) - 1 ): if vetor[j] > vetor[j + 1 ]: aux = vetor[j] vetor[j] = vetor[j + 1 ] vetor[j + 1 ] = aux print(vetor) def bublesort_melhorado1(): array = prenche_vetor() print(array) for i in range( 0 , len(array) - 1 ): for j in range(len(array) - 1 , i, - 1 ): if array[j] < array[j - 1 ]: aux = array[j] array[j] = array[j - 1 ] array[j - 1 ] = aux print(array) def bublesort_melhorado2():

Aqui oque fazemos é prdenar de forma decrescente

array = prenche_vetor() print(array) n = 1 troca = 1 while(n<= 5 and troca == 1 ): troca = 0 for i in range( 0 , len(array) - 1 ): if array[i] < array[i + 1 ]: troca = 1 aux = array[i] array[i] = array[i + 1 ] array[i + 1 ] = aux n = n + 1 print(array) def main(): print('Algoritimo BubleSort')

bublesort() print() print('Algoritimo BubleSort Melhorado versão 1') bublesort_melhorado1() print() print('Algoritimo BubleSort Melhorado versão 2') bublesort_melhorado2() if name == 'main': main()