-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion.py
32 lines (24 loc) · 911 Bytes
/
insertion.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
from utils import my_length
# wrapper
def insertion_sort(my_list, left=0, right=None):
my_insertion_opt_1(my_list, left, right)
# insertion sort
def my_insertion_opt_n(my_list, left=0, right=None):
left, right, length = my_length(my_list, left, right)
if length > 0:
for i in range(left, right+1):
for j in range(i, left, -1):
if my_list[j - 1] > my_list[j]:
my_list[j - 1], my_list[j] = my_list[j], my_list[j - 1]
return
# insertion sort
def my_insertion_opt_1(my_list, left=0, right=None):
left, right, length = my_length(my_list, left, right)
if length > 0:
for i in range(left, right+1):
for j in range(i, left, -1):
if my_list[j - 1] > my_list[j]:
my_list[j - 1], my_list[j] = my_list[j], my_list[j - 1]
else:
break
return