My friggin loser dad is trying to get me to learn how to code during quarantine so told me to make a random number bubble sort in python. The user is meant to input if they want to see the numbers from biggest to smallest and vice versa and then create a random number list and sort them. Im a bit stuck and don't know where to go. Thanks to those who might help. :)
Heres the code
import random
def bubble_sort(list):
# We go through the list as many times as there are elements
for i in range(len(list)):
# We want the last pair of adjacent elements to be (n-2, n-1)
for j in range(len(list) - 1):
if list[j] > list[j+1]:
# Swap
list[j], list[j+1] = list[j+1], list[j]
correct=False
upordownuni=False
list = []
for i in range(0,100):
x = random.randint(1,10)
list.append(x)
while correct==False:
print("Do you want the list to be sorted up or down?")
upordown = input ("Type up or down for what you want\n")
if upordown==("up"):
upordownuni=True
break
bubble_sort()
elif upordown==("down"):
break
bubble_sort()
else:
print("Invalid! Please input up or down.")
Aucun commentaire:
Enregistrer un commentaire