我試圖使用冒泡排序以降序對鏈表進行排序。我的代碼按字母順序列印出一個串列。我想列印出第二個元素并將其從最高值到最低值而不是按字母順序排序。
我在這里缺少什么?如果有人能指出這一點,我將不勝感激。
class LinkedList:
def __init__(self, data):
self.label = data[0][0]
self.value = data[0][1]
self.tail = None if (len(data) == 1) else LinkedList(data[1:])
countries = [("Ukraine",41879904),("Brunei",442400),("Christmas Island (Australia)",1928),("Mauritius",1265985),("Lesotho",2007201),("Guatemala",16604026),("British Virgin Islands (UK)",30030),("Malta",493559),("Greenland (Denmark)",56081),("Guernsey (UK)",62792)]
# BUBBLE SORT
def bubbleSort(countries):
for passnum in range(len(countries)-1,0,-1):
for i in range (passnum):
for j in range(0, len(countries)-i,-1)
if (countries[j][1] > countries[j 1][1]):
temp = countries[j]
countries[j] = countries[j 1]
countries[j 1] = temp
return countries
print("Bubble Sort")
print("The Unsorted List: ", countries)
print('\n' * 1)
bubbleSort(countries)
print("The Original Sorted List: ", countries)
print('\n' * 1)
countries.reverse()
print("The Updated List in Descending Order: ", countries)
uj5u.com熱心網友回復:
我不確定你為什么需要三個嵌套回圈或者passnum變數是否有任何相關性。我沒有在這個答案中包含它,但如果它很重要,那么更改不會有任何區別。
def bubbleSort(countries):
for i in range(len(countries)):
for j in range(0, len(countries)-i-1):
if countries[j][1] > countries[j 1][1]:
temp = countries[j]
countries[j] = countries[j 1]
countries[j 1] = temp
return countries
輸出
Bubble Sort
The Unsorted List: [('Ukraine', 41879904), ('Brunei', 442400), ('Christmas Island (Australia)', 1928), ('Mauritius', 1265985), ('Lesotho', 2007201), ('Guatemala', 16604026), ('British Virgin Islands (UK)', 30030), ('Malta', 493559), ('Greenland (Denmark)', 56081), ('Guernsey (UK)', 62792)]
The Original Sorted List: [('Christmas Island (Australia)', 1928), ('British Virgin Islands (UK)', 30030), ('Greenland (Denmark)', 56081), ('Guernsey (UK)', 62792), ('Brunei', 442400), ('Malta', 493559), ('Mauritius', 1265985), ('Lesotho', 2007201), ('Guatemala', 16604026), ('Ukraine', 41879904)]
The Updated List in Descending Order: [('Ukraine', 41879904), ('Guatemala', 16604026), ('Lesotho', 2007201), ('Mauritius', 1265985), ('Malta', 493559), ('Brunei', 442400), ('Guernsey (UK)', 62792), ('Greenland (Denmark)', 56081), ('British Virgin Islands (UK)', 30030), ('Christmas Island (Australia)', 1928)]
uj5u.com熱心網友回復:
Python 有一個內置的排序函式,它可能比冒泡排序的自編碼版本更快。
這段代碼:
temp = [("ukraine", 852), ("America", 965), ("China", 785)]
nums = [i[1] for i in temp]
nums.sort(reverse=True)
ordered = []
for num in nums:
for item in temp:
if num == item[1]:
ordered.append(item)
回傳一個排序串列,按降序排列。是否需要使用冒泡排序,還是您只是將其選為排序演算法?
正如 Jasmijn 所指出的,這不是非常有效,并且不能處理資料的完整可能性領域。
這段代碼:
temp = [("ukraine", 852), ("America", 965), ("China", 785), ("Argentina", 965)]
temp.sort(key=lambda a: a[1], reverse=True)
處理資料中的所有可能性,比我的第一個代碼片段更簡潔、更高效。
uj5u.com熱心網友回復:
鏈表上的冒泡排序通過串列交換順序錯誤的連續元素對進行,重復該程序直到不再進行交換。
您可以通過交換元素內容或重新排列鏈接來實作它。
交換內容
class LinkedList:
def __init__(self, data=None,*more):
self.label, self.value = data or [None,None]
self.link = LinkedList(*more) if more else None
def bubbleSort(L):
swapping = True
while swapping: # go through list as many times as needed
swapping = False
a,b = L,L.link # compare successive element pairs
while a and b:
if a.value<b.value: # swap if in wrong order
a.value,b.value = b.value,a.value
a.label,b.label = b.label,a.label
swapping = True
a,b = b,b.link # advance in linked list
樣品用途:
countries = LinkedList(("Ukraine",41879904),("Brunei",442400),
("Christmas Island (Australia)",1928), ("Mauritius",1265985),
("Lesotho",2007201),("Guatemala",16604026),
("British Virgin Islands (UK)",30030),("Malta",493559),
("Greenland (Denmark)",56081),("Guernsey (UK)",62792))
print("linked list")
e = countries
while e:
print(e.label,e.value,end=" --> ")
e = e.link
print("END")
bubbleSort(countries)
print("sorted list")
e = countries
while e:
print(e.label,e.value,end=" --> ")
e = e.link
print("END")
輸出:
linked list
Ukraine 41879904 --> Brunei 442400 --> Christmas Island (Australia) 1928 --> Mauritius 1265985 --> Lesotho 2007201 --> Guatemala 16604026 --> British Virgin Islands (UK) 30030 --> Malta 493559 --> Greenland (Denmark) 56081 --> Guernsey (UK) 62792 --> END
sorted list
Ukraine 41879904 --> Guatemala 16604026 --> Lesotho 2007201 --> Mauritius 1265985 --> Malta 493559 --> Brunei 442400 --> Guernsey (UK) 62792 --> Greenland (Denmark) 56081 --> British Virgin Islands (UK) 30030 --> Christmas Island (Australia) 1928 --> END
重新排列鏈接
操作鏈接的冒泡排序將需要回傳一個新的頭部,因為原始的排序后可能不再是串列中的第一個:
def bubbleSort(L):
head = LinkedList() # need pointer to head
head.link = L # to track if it is swapped
swapping = True
while swapping: # go through list as many times as needed
swapping = False
prev,a,b = head,L,L.link # process needs predecessor of a
while a and b:
if a.value<b.value: # swap if in wrong order
prev.link,b.link,a.link = b,a,b.link # rearrange links
prev,a,b = b,a,a.link # avance through list
swapping = True
else:
prev,a,b = a,b,b.link # avance through list
return head.link # return new head
用法:
countries = LinkedList(("Ukraine",41879904),("Brunei",442400),
("Christmas Island (Australia)",1928), ("Mauritius",1265985),
("Lesotho",2007201),("Guatemala",16604026),
("British Virgin Islands (UK)",30030),("Malta",493559),
("Greenland (Denmark)",56081),("Guernsey (UK)",62792))
countries = bubbleSort(countries) # returns new head of the (sorted) list
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/314592.html
下一篇:如何對多個物件的屬性進行排序
