Here\'s a Python implementation of insertion sort, I tried to follow the values on paper but once the counting variable i gets bigger than len(s) I don\'t know what to do, h
The python range(start, end)
function starts counting from start
to end - 1
. That is, end
will never be part of the range()
values. So if you have, for example, range(len(A))
, and A
is an array (in Python, a list) of 10 integers, len(A)
will be 10, and range(len(A))
will return (0,1,2,3,4,5,6,7,8,9)
so you can index every element in A.
In your case, i never gets bigger than len(s) - 1
.
Following your code on paper can be useful, but you have to make sure that the programming language does exactly what you think it does, and sometimes the implementation isn't intuitive. A fast and simple way of tracing your program values is to use print
statements. For example:
def sort_numbers(s):
for i in range(1, len(s)):
# let's see what values i takes on
print "i = ", i
val = s[i]
j = i - 1
while (j >= 0) and (s[j] > val):
s[j+1] = s[j]
j = j - 1
s[j+1] = val
__author__ = 'Dharmjit'
def InsertionSort(list):
for index in range(1,len(list)):
curr = list[index]
position = index
while position > 0 and list[position-1] > curr:
list[position] = list[position-1]
position = position - 1
list[position] = curr
return list
l = [2,1,5,3,9,6,7]
print(InsertionSort(l))
[1,2,3,5,6,7,9]
You can see the whole concept here- http://pythonplanet.blogspot.in/2015/07/sorting-algorithm-1-insertion-sort.html
A simple combination of list slicing and bubble sort
s = [54,26,93,17,77,31,44,55,20]
for i in range(1,len(s)+1):
b = s[0:i]
a = b
count = 0
while count<len(a): # while loop to perform the for loop len(a) number of times-like in bubble sort
for j in range(len(a)-1): # for loop compares every j'th element with next element
if a[j] >= a[j+1-len(a)]:
temp = a[j]
a[j] = a[j+1-len(a)]
a[j+1-len(a)] = temp
count = count+1
print a
I have looked through a number of these posts and i think that people are over complicating this issue. please correct me if i have done this wrong but this is my interpretation of the sort.
def insert(L): # this creates the definition and assines the list that you input to L
for i in range(len(L)): # this then loops the next code for the length of the list
while i > 0 and L[i] < L[i-1]: # this is the main part of the code where it checks
# if the value from the list is less than the one before it and if it is then it
# swaps them around
L[i-1], L[i] = L[i], L[i-1] # this code just swaps round the values
print (L)# prints the list out at the end of each part
L = [5,2,3,7,6,9,7]
insert(L)
Using this it outputs:
[2, 5, 3, 7, 6, 9, 7]
[2, 3, 5, 7, 6, 9, 7]
[2, 3, 5, 6, 7, 9, 7]
[2, 3, 5, 6, 7, 7, 9]
The print function can also be removed from line and can be placed outside the for loop so that it only prints the list at the end. Which would only give the last line:
[2, 3, 5, 6, 7, 7, 9]
+An alternative with two for loops and descriptive names.
def insertionSort(list):
for outerIndex in range(len(list)):
for innerIndex in range(outerIndex, 0, -1):
if list[innerIndex] >= list[innerIndex - 1]:
break
list[innerIndex], list[innerIndex - 1] = list[innerIndex - 1], list[innerIndex]
return list
print insertionSort([3, 1e-10, 7e5, 2.3, 100, -2.5, 12.1])
# => [-2.5, 1e-10, 2.3, 3, 12.1, 100, 700000.0]
def insertie(x):
nrc=0
nrm=0
for i in range(0,len(x)):
aux=x[i]
nrm=nrm+1
j=i-1
while j >= 0 and aux < x[j]:
nrc=nrc+1
x[j+1]=x[j]
nrm=nrm+1
j=j-1
nrc=nrc+1
x[j+1]=aux
nrm=nrm+1
return x
x=[7,5,4,9,1,4,0,1]
print insertie(x)