使用冒泡排序,如何使用第二个元素以便按降序打印
Using Bubble Sort, how can I use the second element so I can print in descending order
我尝试使用冒泡排序对链表进行降序排序。我的代码改为按字母顺序打印出一个列表。我想打印出第二个元素并将其从最高值到最低值而不是字母顺序排序。
我在这里错过了什么?如果有人能指出这一点,我将不胜感激。
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)
我不确定您为什么需要三个嵌套循环,或者 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)]
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)
returns 一个排序列表,按降序排列。
是否需要使用冒泡排序,还是您只是选择它作为排序算法?
正如 Jasmijn 所指出的,这不是非常有效,并且无法处理数据的全部可能性。
此代码:
temp = [("ukraine", 852), ("America", 965), ("China", 785), ("Argentina", 965)]
temp.sort(key=lambda a: a[1], reverse=True)
处理数据中的所有可能性,比我的第一个代码片段更简洁、更高效。
链表上的冒泡排序通过列表交换顺序错误的连续元素对,重复该过程,直到不再进行交换。
您可以通过交换元素内容或重新排列链接来实现它。
交换内容
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
重新排列链接
操作链接的冒泡排序需要 return 一个新的头部,因为排序后原始头部可能不再是列表中的第一位:
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
我尝试使用冒泡排序对链表进行降序排序。我的代码改为按字母顺序打印出一个列表。我想打印出第二个元素并将其从最高值到最低值而不是字母顺序排序。
我在这里错过了什么?如果有人能指出这一点,我将不胜感激。
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)
我不确定您为什么需要三个嵌套循环,或者 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)]
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)
returns 一个排序列表,按降序排列。 是否需要使用冒泡排序,还是您只是选择它作为排序算法?
正如 Jasmijn 所指出的,这不是非常有效,并且无法处理数据的全部可能性。
此代码:
temp = [("ukraine", 852), ("America", 965), ("China", 785), ("Argentina", 965)]
temp.sort(key=lambda a: a[1], reverse=True)
处理数据中的所有可能性,比我的第一个代码片段更简洁、更高效。
链表上的冒泡排序通过列表交换顺序错误的连续元素对,重复该过程,直到不再进行交换。
您可以通过交换元素内容或重新排列链接来实现它。
交换内容
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
重新排列链接
操作链接的冒泡排序需要 return 一个新的头部,因为排序后原始头部可能不再是列表中的第一位:
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