Deeply...
The sorting algorithm (selective sorting) can be implemented, but the efficiency is slow to O(n2). It is usually very efficient to use functions that are already optimized for the primary module.
2)The sorted function uses the timsort algorithm.
import operator
word_list = [('this', 4), ('they', 3), ('that', 13), ('shall', 3), ('people', 3), ('nation', 5), ('here', 8),
('have', 5), ('great', 3), ('dedicated', 4), ('dead', 3)]
sorted(word_list, key=operator.itemgetter(1), reverse=True)
[('that', 13),
('here', 8),
('nation', 5),
('have', 5),
('this', 4),
('dedicated', 4),
('they', 3),
('shall', 3),
('people', 3),
('great', 3),
('dead', 3)]
word_list = [('this', 4), ('they', 3), ('that', 13), ('shall', 3), ('people', 3), ('nation', 5), ('here', 8),
('have', 5), ('great', 3), ('dedicated', 4), ('dead', 3)]
a = len(word_list)
for i in range(a - 1):
for j in range(a - i - 1):
if word_list[j][1] < word_list[j + 1][1]:
temp = word_list[j]
word_list[j] = word_list[j + 1]
word_list[j + 1] = temp
continue
continue
print(word_list)
Compare the integers in the tuple value one by one.
© 2024 OneMinuteCode. All rights reserved.