[英]Which is more efficient in Python: list.index() or dict.get()
[英]which code is more efficient in bubble sorting in list (python)
myList = [8, 10, 6, 2, 4] # list to sort
i=4
while i>0:
for j in range(i):
if myList[j] > myList[j + 1]:
myList[j], myList[j + 1] = myList[j + 1], myList[j]
i-=1
print(myList)
/* ---------------------------------------------------------------------*/
myList = [8, 10, 6, 2, 4] # list to sort
swapped = True # it's a little fake - we need it to enter the while loop
while swapped:
swapped = False # no swaps so far
for i in range(len(myList) - 1):
if myList[i] > myList[i + 1]:
swapped = True # swap occured!
myList[i], myList[i + 1] = myList[i + 1], myList[i]
print(myList)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.