def check_sort(L):
for i in range(len(L)-1):
if (L[i] > L[i+1]):
return False
return True
#L[x] > L[x+ 2]
#L[x+1] <= L[x+2]
#L[x] <= L[x+1]
select(L,2)
# Suppose we have a list in which the first i elements are
# sorted and the smallest elements in the list.
def select(L, i):
min_index = i
for j in range(i,len(L)):
if L[j] < L[min_index]:
min_index = j
temp = L[i]
L[i] = L[min_index]
L[min_index] = temp
return #
def insert(L, i):
x = L[i]
insertion_index = i
for j in range(i-1, -1, -1):
if L[j] > L[j+1]:
tmp = L[j]
L[j] = L[j+1]
L[j+1] = tmp
else:
return
return
#L.pop(i)
#L.insert(x, insertion_index)
[ second_biggest_elt, max_elt]
def bubble(L, i):
for j in range(len(L) - 1 - i):
if L[j] > L[j+1]:
tmp = L[j]
L[j] = L[j+1]
L[j+1] = tmp
i = 0
while (not bubble(L,i)):
i+=1
def bubble(L, i):
list_sorted = True
for j in range(len(L) - 1, i-1):
if L[j] > L[j+1]:
tmp = L[j]
L[j] = L[j+1]
L[j+1] = tmp
list_sorted = False
return list_sorted
#X = [-1,0, 1,3, -7, 4]
#insert(X, 3)
#X == [1,1.5,2,3,4,-7]
Suppose we have a list in which the first i elements are sorted and the smallest elements in the list.
What can we do to make sure the first i+1 elements are sorted and the smallest elements within the list?
How long does this take?