set list start at 1 python 1

set list start at 1 python

A = ['dummy',5,2,6,4,1,3]
for j in range(2,len(A)):
    key = A[j]
    i=j-1
    while i>0 and A[i]>key:
        A[i+1] = A[i]
        i = i-1
    A[i+1] = key
A.pop(0)
print A

Here is what the above code is Doing:
1. We start with the second element in the list, and we call it the key.
2. We then compare the key with the element to its left. If the key is smaller, we swap the two elements.
3. We then compare the key with the element to its left. If the key is smaller, we swap the two elements.
4. We continue this process until the key is no longer smaller than the element to its left.
5. We then move on to the third element in the list, and repeat the process.
6. We continue this process until we reach the end of the list.

Similar Posts