-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathexercise3b.py
54 lines (39 loc) · 1.17 KB
/
exercise3b.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
42
43
44
45
46
47
48
49
50
51
52
53
54
""" Sorting
Write a function that takes a list of numbers as parameter, and returns the list sorted.
- Hint: use `list.sort()` or implement on your own sorting (give it a try!)
"""
def Sort(my_list):
return sorted(my_list)
print(Sort([2, 1, 4]))
def insert_sort(lists):
# insert sort
count = len(lists)
for i in range(1, count):
key = lists[i]
j = i - 1
while j >= 0:
if lists[j] > key:
lists[j + 1] = lists[j]
lists[j] = key
j -= 1
return lists
print(insert_sort([2, 1, 4]))
def bubble_sort(lists):
# Bubble sort
count = len(lists)
for i in range(0, count):
for j in range(i + 1, count):
if lists[i] > lists[j]:
lists[i], lists[j] = lists[j], lists[i]
return lists
def select_sort(lists):
# select sort
count = len(lists)
for i in range(0, count):
min = i
for j in range(i + 1, count):
if lists[min] > lists[j]:
min = j
lists[min], lists[i] = lists[i], lists[min]
return lists
print(select_sort([4, 2, 1]))