-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.py
42 lines (32 loc) · 997 Bytes
/
BubbleSort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import random
rand_data = [random.randint(0, 100) for i in range(10)]
def shift(x: int, array: list):
array[x], array[x + 1] = array[x + 1], array[x]
def bubble_sort(array: list):
sorted_list = list(array)
for i in range(0, len(sorted_list) - 1):
change = False
for j in range(0, len(sorted_list) - i - 1):
if sorted_list[j] > sorted_list[j + 1]:
shift(j, sorted_list)
change = True
if not change:
break
return sorted_list
def some_sort(array: list):
sorted_list = list(array)
index = 0
while index < len(sorted_list) - 1:
if sorted_list[index] > sorted_list[index + 1]:
shift(index, sorted_list)
if index > 0:
index -= 1
else:
index += 1
return sorted_list
print("Sortierte Liste:")
print(some_sort(rand_data))
print("\nRandom Liste:")
print(rand_data)
print("\nBubblesort:")
print(bubble_sort(rand_data))