I have this psuedocode:

FUNCTION initialize
FOR row = 1 to num_items
get random number for the list
ENDFOR
ENDFUNCTION initialize

FUNCTION show
FOR row = 1 to num_items
show output: list(row)
ENDFOR
ENDFUNCTION show

FUNCTION sort
FOR pass = 1 to num_items
posn = 1
sorted = True
WHILE posn < num_items
IF list(posn) > list(posn + 1)
swap list(posn) with list(posn + 1)
sorted = False
ENDIF
increment posn
ENDWHILE
IF sorted
Exit the FOR loop
ENDIF
perform show
ENDFOR
ENDFUNCTION sort

num_items = MAX
define a numeric list of size num_items

perform initialize
perform show
perform sort

That I need to translate into python. so far this is what I have:

from random import randint
list_num=[]
row=0
num=0
num_items=15
def initialize():
    for row in range(0,num_items):
        number=randint(1,1000)
        list_num.append(number)
        row +=1
def sorter():
    posn=1
    for num in range(0,num_items):
        srtd=0
        while posn<num_items:
            if list(posn) >list(posn+1):
                a= list_num.index(list_num(posn))
                b= list_num.index(list_num(posn+1))   
                list_num[b],list_num[a]=list_num[a],list_num[b]
                srtd=1
                posn+=1
            else:
                posn+=1
        if srtd==0:
            break
    print list_num
initialize()
print list_num
sorter()

But I keep getting this result:

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> [evaluate bubble.py]
[691, 577, 8, 960, 214, 483, 841, 373, 481, 569, 785, 442, 118, 538, 289]
TypeError: iteration over non-sequence
>>>

Any help or advice you have to offer would be a great deal of help.

Thanks,


Jaro

Recommended Answers

All 7 Replies

You are mixing list object with list access by indexing. You can not construct list from integer but only from iterable.

list(posn) in pseudo code means name_of_list[posn] in Python (or posn-1 if indexes must be made zero based from one based)

lines 3, 4 and 10 also have no purpose or effect.

You maybe want to compare with http://www.daniweb.com/software-development/python/threads/35962/176799#post176799 or similar threads in Daniweb.

Looks like the name of your program you are trying to do a bubblesort not sure if you need to follow a specific format or anyting but this seems to work and may give you some idea of where you are going wrong or give you some ideas.

from random import randrange


def initialize():
    """ create a list of random numbers and sort them and print them out"""
    #creates the list
    tmp = []
    mylist = []
    for num in range(0,15):
        mylist.append(randrange(1,1000))

    #keep sending the list to the sorter always returning the
    #highest number back until initial list is empty
    while mylist != []:
        highest = sorter(mylist)
        tmp.append(highest)
        mylist.remove(highest)
    #print to screen
    print "high to low\n" , tmp
    tmp.reverse()
    print "low to high\n" , tmp


def sorter(mylist):
    """ find the highest number and return it"""
    highest = 0
    for item in mylist:
        if item > highest:
            highest = item
    return highest


initialize()

That is not bubble sort, predator78. Bubble sort is swapping sort.

Yeah I figured it might not be I havn't really worked with any algorithms in sometime. Just threw that out as a working example working with lists.

I've tweaked my code a bit and now I have this code:

from random import randint
unsorted_list=[]
num_items=15
list_num=[]
def initialize():
    for row in range(0,num_items):
        number=randint(1,1000)
        unsorted_list.append(number)
    print unsorted_list
        
def sorter(unsorted_list):
    sorted = False
    while not sorted:
        sorted = True
        for i in range(1,num_items):
            if unsorted_list[i] > unsorted_list[i+1]:
                sorted = False
                unsorted_list[i], unsorted_list[i+1] = unsorted_list[i+1], unsorted_list[i]
initialize()
sorter(unsorted_list)

My problem now is that I get this error:

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> [evaluate bubble.py]
[363, 51, 731, 238, 548, 92, 727, 409, 944, 76]
IndexError: list index out of range
>>>

And it is highlighting line 15 in red

Thanks for the help,


Jaro

tweak the range in line 15 correct considering unsorted_list[i+1].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.