Skip to content

Commit f334ac1

Browse files
author
dodo
committed
beispielslösung bubblesort
1 parent 9d344a2 commit f334ac1

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Level_03/Aufgaben/bubblesort.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
import random
3+
4+
def get_random_list(n: int) -> list:
5+
result = list(range(n))
6+
random.shuffle(result)
7+
return result
8+
9+
n = int(input("Länge der Liste: "))
10+
unsorted_list = get_random_list(n)
11+
12+
# Bitte die Zeilen 1-12 unverändert lassen
13+
14+
def bubblesort(unsorted_list):
15+
changed = True
16+
while changed:
17+
changed = False
18+
for i in range(len(unsorted_list)-1):
19+
if unsorted_list[i] > unsorted_list[i+1]:
20+
unsorted_list[i], unsorted_list[i+1] = unsorted_list[i+1], unsorted_list[i]
21+
changed = True
22+
23+
print(unsorted_list)
24+
bubblesort(unsorted_list)
25+
print(unsorted_list)

0 commit comments

Comments
 (0)