I cant pass the 6th test: “Your selection_sort should follow the selection sort algorithm, swapping the minimum value in unsorted part of the list with first the unsorted element.” I don’t understand why my code doesn’t meet this requirement though.
def selection_sort(arr):
n = len(arr)
for i in range(n):
# Step 1: assume the first unsorted element is the minimum
min_index = i
# Step 2: find the minimum element in the unsorted portion
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
# Step 3: swap the minimum element with the first unsorted element
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr
_list = [10, 1, 2, 3, 4, 5]
print(‘Unsorted array:’)
print(_list)
_sorted_list = selection_sort(_list)
print(‘Sorted array:’)
print(_sorted_list)
Your indentation does not look correct for Python
Can you post a link to the lab?
In the future you can use the “get help” button
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
Thank you.
The code showed up weird when I copied it into the forum
Can you paste your code here and keep the indentation intact?
It’s pretty important to understand the program and debug.
If you paste it in, the indentation will be there. Make sure to surround it with backticks or select it and click the code/preformatted text button.
Make sure markdown is selected.
I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add the backticks.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
Use print() to output the array each time it’s updated so you can see the changes.
- Your
selection_sortshould follow the selection sort algorithm, swapping the minimum value in unsorted part of the list with first the unsorted element.
What if the current number is also the lowest number? Should they swap?

