Implement selection sort

Hello, I’m implementing the selection sort. my implementation passes the tests but I think mine isn’t exactly selection sort. can anyone help?
here’s my code:

def selection_sort(items):
items = items[:]
result = []
while len(items):
    result.append(min(items))
    items.remove(min(items))

return result

This code, as shown, does NOT pass the tests.

Can you explain why you think it’s not a selection sort?

It does, when I was copying the code, somehow the indentation changed, here’s the correct code:

def selection_sort(items):
    items = items[:]
    result = []
    while len(items):
        result.append(min(items))
        items.remove(min(items))

    return result

because instead of swapping, I’m just adding it to a new list and also, I’m using python built-in min instead doing a for loop to find the minimum thing there.

1 Like

I assumed as much, but thank you for the clarification.

I see what you mean. It is close, like you’re breaking the list out into 2 lists, but there’s no still “swap”. I guess the tests are mainly checking that you output a sorted list, which you do.

Don’t worry about using min() I think that’s a good idea and it’s not forbidden by the instructions. Doing this in a loop is just extra legwork for no reason.

You’ve satisfied the instructions and the tests, so feel free to move on. I encourage you to re-write your program to do the swap though, I think that would be good practice and satisfying.

Good catch though, I’m going to open an Issue about this lab.

I’ve opened an Issue here: https://github.com/freeCodeCamp/freeCodeCamp/issues/64360

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

1 Like