Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sort_with_select(a_list):
- for i in range(len(a_list)):
- minIndex = i
- for j in range(i + 1, len(a_list)):
- #my answer - did the same way as bubble sort
- '''
- if a_list[j] < a_list[i]:
- temp = a_list[i]
- a_list[i] = a_list[j]
- a_list[j] = temp
- '''
- #Model answer
- #The loops above already take care of the overall control flow of selection sort: all we need to do here is see if the current item is lower than the lowest-found item on the current iteration. So, we check the current item against the lowest-found item so far:
- if a_list[j] < a_list[minIndex]:
- #And if it's lower, we note that this is the new lowest-found item:
- minIndex = j
- minValue = a_list[minIndex] #the smaller no. is j, so assign this as the minValue
- del a_list[minIndex] #remove the smaller no. value and insert it to the i position
- a_list.insert(i, minValue)
- return a_list
- print(sort_with_select([5, 3, 1, 2, 4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement