Python 3 氣泡排序.test

剛學Python 測試一下str 氣泡排序

線上測試:

Online Python Compiler (Interpreter) (programiz.com)

def bubbleSort(arr):
n = len(arr)
for i in range(n):

# 讓最後一個完成
for j in range(0, n-i-1):

if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]

x = input("請輸入:").split(", ")
print("存成陣列:", x)
bubbleSort(x) #執行氣泡排序

print ("氣泡排序完成:")
for i in range(len(x)):
# print ("%s" %x[i]) #不同寫法
print (x[i])

# 輸入
#64, 34, 25, 12, 22, 11, 64
# 輸出
# 氣泡排序完成:
# 11
# 12
# 22
# 25
# 34
# 64
# 64
# 補充
#len(n) 長度length
#range(n) 範圍內跑一次
#split(", ") 文字依照" "分割成陣列
結果

--

--

--

這邊主要分享自己查資料找不到的資訊,若之後有人遇到相同的問題便可省去不少時間。

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
亂叫阿醇

亂叫阿醇

這邊主要分享自己查資料找不到的資訊,若之後有人遇到相同的問題便可省去不少時間。

More from Medium

Real Python

Learning Python in 2022 — the no doubt way

[Python learning journal] Day 4