def imprime(numeros): print str(numeros)[1:-1], "\n"; # Procedimento responsável por ordenar o array, utilizando o algoritmo Bubble Sort. # No fim do procedimento os números contidos no array estarão em ordem numérica. def ordena(numeros): houveTroca = True; while (houveTroca): houveTroca = False; for i in range(0, len(numeros)-1): # compara o elemento atual (i) c/ o proximo (i+1) if (numeros[i] > numeros[i+1]): #faz a troca aux = numeros[i]; numeros[i] = numeros[i+1]; numeros[i+1] = aux; #indica que o array deve ser analisado novamente (while) houveTroca = True; # modifique o conteúdo do vetor para testar o programa vetor = [7, 2, -4, 0, 10, 1, 4, 3]; print "\nConteúdo do array antes de ordenar..."; imprime(vetor); # coloca os valores do array em ordem numérica (ascende) ordena(vetor); print "\nConteúdo do array depois de ordenar..."; imprime(vetor);
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question