插入排序算法用Python的两种表示方法
def insert_sort(arr):
if len(arr) == 1:
return arr
for i in range(1, len(arr)):
for j in range(i, 0, -1):
if arr[j] < arr[j - 1]:
arr[j], arr[j - 1] = arr[j - 1], arr[j]
return arr
def insert_sort2(arr):
if len(arr) == 1:
return arr
for i in range(1, len(arr)):
temp = arr[i]
j = i - 1
while j >= 0 and arr[j] > temp:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = temp
return arr
网友评论