I tried implementing insertion sort in python. 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, how/why does it still run?

```def sort_numbers(s):
for i in range(1, len(s)):
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

def main():
x = eval(input("Enter numbers to be sorted: "))
x = list(x)
sort_numbers(x)
print(x)```
Oct 8, 2018 in Python 312 views

## 1 answer to this question.

Or, this one:

```def ins_sort(k):
for i in range(1,len(k)):    #since we want to swap an item with previous one, we start from 1
j = i                    #bcoz reducing i directly will mess our for loop, so we reduce its copy j instead
temp = k[j]              #temp will be used for comparison with previous items, and sent to the place it belongs
while j > 0 and temp < k[j-1]: #j>0 bcoz no point going till k since there is no seat available on its left, for temp
k[j] = k[j-1] #Move the bigger item 1 step right to make room for temp
j=j-1 #take k[j] all the way left to the place where it has a smaller/no value to its left.
k[j] = temp
return k```
• 7,720 points

## How does % work in Python?

The % (modulo) operator yields the remainder ...READ MORE

## How does function definition work in python?

Is it same as other programming languages ...READ MORE

## How does pillow work in opening and closing images in python?

Is there a separate library that we ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

## Lowercase in Python

You can simply the built-in function in ...READ MORE