-1

I am trying to create an original sorting algorithm for my home work for school but i can't get it to work and I don't understand why.

def sa(x):
    print(x)
    i = 0
    s_array = []

    while x:
        for x[i] in x:
            if x[i] == min(x):
                s_array.append(x.pop(i))
                i = 0
            elif x[i] == max(x):
                s_array.append(x.pop(i))
                i = 0
            else:
                i += 1

    print(s_array)

I know I shouldn't have print functions in the program but i wanted it to be concise and packaged. so this is the output i get:

>>> sa([89, 23, 33, 45, 10, 12, 45, 45, 45])
[89, 23, 33, 45, 10, 12, 45, 45, 45]
[89, 45, 45, 45, 45, 45, 45, 45, 12]

I had the code like this:

def sa(x):
    print(x)
    i = 0
    s_array = []

    while x:
        for x[i] in x:
            if x[i] == min(x):
                s_array.append(x.pop(i))
                i = 0
            else:
                i += 1

print(s_array)

but was getting this as the output and thought i had to add the max() elif:

>>> sa([89, 23, 33, 45, 10, 12, 45, 45, 45])
[89, 23, 33, 45, 10, 12, 45, 45, 45]
[10, 45, 45, 45, 45, 45, 45, 45, 45]

please help.

4
  • 1
    for x[i] in x: is assigning x[i] to each element of x as it is iterated. Absolutely certain that isn't what you are intending. Commented Jan 25, 2017 at 0:26
  • if i = 0 doesn't the for loop statement x[i] assume x[0]? Commented Jan 25, 2017 at 0:46
  • There is no point of a for loop and a while loop in this case. You should pick one, for sorting you probably want the while though. Also you can do, while x: s_array.append(x.pop(x.index(min(x)))). Commented Jan 25, 2017 at 0:53
  • thank you @StevenSummers and for helping me out. taking the for loop out and using the while loop you provided works perfectly. Commented Jan 25, 2017 at 1:14

1 Answer 1

0

the way your for loop is written you are assigning x[i] to an element in x according to iteration, consider this:

i = 0
x = ["a","b","c","d","e"]

for x[i] in x:
    print(x)

This will display the following:

['a', 'b', 'c', 'd', 'e']
['b', 'b', 'c', 'd', 'e']
['c', 'b', 'c', 'd', 'e']
['d', 'b', 'c', 'd', 'e']
['e', 'b', 'c', 'd', 'e']

Notice that the first element x[0] is changing to each element of the list as the list is iterated over, and when i changes it basically mixes up the elements of the list in a predictable but rarely useful way. instead since you only want to loop while there is still elements in the list just remove the inner for loop:

while x:
    if x[i] == min(x):
        s_array.append(x.pop(i))
        i = 0
    else:
        i += 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.