added python bubble sort
This commit is contained in:
parent
d20c443088
commit
e5f4774ee6
46
2.Erste Programme/3. BubbleSort Python/main.py
Normal file
46
2.Erste Programme/3. BubbleSort Python/main.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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()
|
Loading…
x
Reference in New Issue
Block a user