我不确定为什么我的插入排序不起作用。它是用python编码的。当我尝试测试输入时,我得到 [4]。
def insertion_sort(list):
q =0
temp = [] #list to hold sorted values
size = len(list)
while(q < size):
if not temp: #if empty add first element of list
temp.append(list[0])
list = list[1:len(list)] #update list so it doesn't include first element
for i in range(1,len(temp)): #insertion step
if(len(temp)==1):
if(list[0] > temp[0]): #if temp is size 1 insert element before/after
temp.append(list[0])
else:
temp.insert(0,list[0])
else:
if(list[0] >= temp[i-1] and list[0] <= temp[i]): #insert value between two values
temp.insert(i,list1[0])
if(list[0] <= temp[0]): # if less than min insert first
temp.insert(0,list1[0])
if(list[0] >= temp[len(temp)-1]): # if greater than max, insert last
temp.insert(len(temp),list[0])
q=q+1
return temp
list = [4,3,2,1]
print insertion_sort(list)