Hi guys,

I got this from my teacher and im really confused. could anyone point me in the right direction?

Thanks

What does it do?

This time I'm looking for an overall summary, in one sentence, with the word "by" in the middle:

"This function ..........., by ................ ."

This function takes a list of numbers as an argument, and returns a list of numbers.
To help you figure it out, cut out 5 or 6 pieces of paper, write some numbers on them, put them on a table, and run the algorithm yourself.

define MyFunction (list):
    for OuterForLoopCounter in 1 to listlength-1:
        for InnerForLoopCounter in 1 to listlength-1:
            if list[InnerForLoopCounter] is greater than list[InnerForLoopCounter+1]:
                swap list[item] and list[item+1]
    return (list)

Recommended Answers

All 3 Replies

Look articles on sorting algorithm based on swapping adjacent items.

Let me give you a head start:

def myfunc(l):
    for i in range (1, len(l)-1):
        for j in range (1, len(l)-1):

for

define MyFunction (list):
    for OuterForLoopCounter in 1 to listlength-1:
        for InnerForLoopCounter in 1 to listlength-1:

now, how about implementing the rest:

 if list[InnerForLoopCounter] is greater than list[InnerForLoopCounter+1]:
                swap list[item] and list[item+1]
    return (list)

Also, for the swaping part, Python allowes this too:

>>> i, j = 1, 2
>>> i
1
>>> j
2
>>> 

so swaping is really easy.

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.