Skip to content

Commit be78520

Browse files
committed
sorting
1 parent f8fd6a6 commit be78520

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

bubble_sort.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def bubble_sort(L):
2+
print("unsorted:", L)
3+
for i in range(len(L)):
4+
for j in range(len(L)-1-i):
5+
if L[j]>L[j+1]:
6+
L[j], L[j+1] = L[j+1], L[j]
7+
print(L)
8+
else:
9+
print(L)
10+
print()
11+
return L
12+
13+
L = [5,4,6,9,2,1]
14+
sort = bubble_sort(L)
15+
print("sorted:", sort)

insertion_sort.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def insertion_sort(L):
2+
print("unsorted:", L)
3+
for i in range(1,len(L)):
4+
current_element = L[i]
5+
6+
while current_element < L[i-1] and i > 0:
7+
L[i] = L[i-1]
8+
i = i - 1
9+
L[i] = current_element
10+
return L
11+
12+
L = [5,4,6,9,2,1]
13+
sort = insertion_sort(L)
14+
print("sorted:",sort)
15+

0 commit comments

Comments
 (0)