Translating pseudocode into Python code : This is the pseudocode I was given:

COMMENT: define a function sort1 

INPUT: a list of numbers my list

print the initial list

loop over all positions i in the list; starting with the second element (index 1)

COMMENT: at this point the elements from 0 to i-1 in this list are sorted
loop backward over those positions j in the list lying to the left of i ; starting at position i-1 continue this loop as long as the value at j+1 is less than the value at j

swap the values at positions j and j+1 

print the current list

And this is the python code I came up with:



    >     #define a function sort1
    >     my_list=range(1,40)
    >     print
    >     print my_list
    >     num_comparisons=0
    >     num_swaps=0
    >     for pos in range (0,len(my_list)-1):
           for i in range(pos+1,len(my_list)): # starting at position i-1 continue this loop as long as the value at j+1 is less than the value at j
           num_comparisons+=1
           if my_list[i]<my_list[pos]:
                num_swaps+=1
                [my_list[i],my_list[pos]]=[my_list[pos],my_list[i]]
    
    >     print my_list
    >     print
    >     print num_comparisons, num_swaps


I'm not sure I did it correctly though