Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Working on implementing this algorithm using Python. I thought my logic was okay but apparently not as Python is complaining. The while loop is causing issues. If I remove that it works as expected but obviously doesn't sort the whole list. My thought process -> Use Linear Search to find the smallest number -> Append that new number to a list -> Remove that number from the current list -> Loop through that same list (but with smallest number removed) again -> Repeat process until we've gone through the entire list "x" number of times. "x" being equal to the length of the list. The problem I think I'm running into is that the list never gets updated every time I run through the for loop? I keep getting the error Line 21: ValueError: list.index(x): x not in list. Even though "x" is in the list. Any idea as to what I'm doing wrong?

"""
Selection sort algorithm.
"""

import random
ls = []
max_number = 10
while len(ls) < max_number:
    ls.append(random.randint(1,101))
print ls   

def selection_sort(items_to_sort):
    smallest_number = items_to_sort[0]
    current_number = 0
    sorted_items = []
    item_len = len(items_to_sort)
    while item_len > 0:
        for item in items_to_sort[:]:
            if item < smallest_number:
                smallest_number = item
        items_to_sort.pop(items_to_sort.index(smallest_number))    
        sorted_items.append(smallest_number)   
        item_len -= 1    
    return sorted_items
print selection_sort(ls)
share|improve this question
up vote 2 down vote accepted

It looks like you're not re-initializing the smallest_number variable, so after the first execution of your while loop - you look for a value smaller than the previous value which you just poped from the list.

When you don't find a value smaller than the previously smallest value which is no longer in the list, you try to pop the same smallest_number as in the previous iteration of the while loop. However, that value is no longer in the items_to_sort list which is why you get the ValueError

Try moving the line smallest_number = items_to_sort[0] to be the first line executed in every iteration of your while loop.

share|improve this answer
    
yes u are right, if he doesn't re-initialize the smallst_number, he can never find a smaller number which is smaller than the last smallest number... – Acepcs 5 hours ago
    
Fantastic. You are a God among men. Thank you good sir/mam. – terratunaz 5 hours ago
    
Glad I could help, if this answer helped you should mark it as accepted. :) – ctj232 5 hours ago

After every while loop, you should assign items_to_sort[0] to smallest_number

current_number = 0
sorted_items = []
item_len = len(items_to_sort)
while item_len > 0:
    smallest_number = items_to_sort[0]
    for item in items_to_sort[:]:
        if item < smallest_number:
            smallest_number = item
    index=items_to_sort.index(smallest_number)
    items_to_sort.pop(index)
    print(items_to_sort)
    sorted_items.append(smallest_number)
    item_len -= 1
return sorted_items
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.