using python Write a program to sort a list of value from largest to smallest us
ID: 3587352 • Letter: U
Question
using python
Write a program to sort a list of value from largest to smallest using Selection Sort. The algorithm should sort-in-place and not create a second array of sorted values
Challenge write a second function that sorts the list, but keeps the original list unsorted and returns a new sorted list. Hint: one approach is to create a copy of the original list and then sort it
Use the following main to test the code
# ------- main ----------
a = [4,2,7,6,3,4,1,9,7,6,2]
mysort(a)
print(a)
b = ["alpha", "sierra", "victor", "echo", "millie" ]
mysort(b)
print(b)
Explanation / Answer
def mysort(a):
for i in range(len(a)):
j = i + 1
while (j < len(a)):
if a[j] > a[i]:
temp = a[i]
a[i] = a[j]
a[j] = temp
j = j+1
a = [4,2,7,6,3,4,1,9,7,6,2]
mysort(a)
print(a)
b = ["alpha","sierra","cictoe","echo","millie"]
mysort(b)
print(b)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.