46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
|
import math
|
||
|
import random
|
||
|
|
||
|
# BubbleSort
|
||
|
|
||
|
def askUser():
|
||
|
print("Random Numbers: Yes / No")
|
||
|
RandomNumbersinput = input()
|
||
|
if RandomNumbersinput == "Yes":
|
||
|
randomNumbers()
|
||
|
elif RandomNumbersinput == "No":
|
||
|
userNumbers()
|
||
|
else:
|
||
|
print("Wrong Input")
|
||
|
askUser()
|
||
|
def randomNumbers():
|
||
|
print("How many numbers do you want to sort?")
|
||
|
number = int(input())
|
||
|
list = []
|
||
|
for i in range(number):
|
||
|
list.append(random.randint(0,100))
|
||
|
print(list)
|
||
|
bubbleSort(list)
|
||
|
print(list)
|
||
|
def userNumbers():
|
||
|
print("How many numbers do you want to sort?")
|
||
|
number = int(input())
|
||
|
list = []
|
||
|
for i in range(number):
|
||
|
print("Enter number:")
|
||
|
list.append(int(input()))
|
||
|
print(list)
|
||
|
bubbleSort(list)
|
||
|
print(list)
|
||
|
def bubbleSort(list):
|
||
|
# BubbleSort
|
||
|
# first loop
|
||
|
for i in range(len(list)):
|
||
|
# second loop
|
||
|
for j in range(len(list)-1):
|
||
|
if list[j] > list[j+1]:
|
||
|
list[j], list[j+1] = list[j+1], list[j]
|
||
|
return list
|
||
|
|
||
|
# Main
|
||
|
askUser()
|