Search code examples
pythonpseudocode

Translating pseudocode into Python


This is the pseudocode I was given:

  1. COMMENT: define a function sort1

  2. INPUT: a list of numbers my list

  3. print the initial list

  4. 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

  5. swap the values at positions j and j+1

  6. 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.


Solution

  • As I said in a comment, I think you effectively do have the j (as well as the i) the pseudocode COMMENT talks about. However, in your code, i is the variable pos, which would make the j what is named i in your code.

    To see if your code works, you need to initially have an unsorted list—not the my_list=range(1,40) in your code (which is [1, 2, 3, ... 38, 39] and already in numerical order).

    One thing you didn't do is define a sort1() function.

    What is below is essentially your code, but I renamed the two variables to match the pseudocode COMMENT, and put (most of it) in a function definition where it's supposed to be.

    Beyond that, I had to declare the variables num_comparisons and num_swaps (which aren't mentioned in the psuedocode) as global so they could be accessed outside of the function—otherwise they would have been local variables by default and only accessible within the function.

    def sort1(items):
        """ Sort given list of items in-place. """
        # allow access to these variables outside of function
        global num_comparisons
        global num_swaps
    
        # initialize global variables
        num_comparisons = 0
        num_swaps = 0
    
        # starting at position i-1 continue this loop as long
        # as the value at j+1 is less than the value at j
        for i in range(0, len(items)-1):
            for j in range(i+1, len(items)):
                num_comparisons += 1
                if items[j] < items[i]:
                   num_swaps += 1
                   [items[j], items[i]] = [items[i], items[j]]
    
    my_list = [6, 3, 7, 2, 9, 4, 5]
    
    print 'my_list before sort:'
    print my_list
    sort1(my_list)
    print 'my_list after sort:'
    print my_list
    print
    print 'num_comparisons:', num_comparisons, ', num_swaps:', num_swaps
    

    Output:

    my_list before sort:
    [6, 3, 7, 2, 9, 4, 5]
    my_list after sort:
    [2, 3, 4, 5, 6, 7, 9]
    
    num_comparisons: 21 , num_swaps: 10