feat: add selection sort lab (#60591)

This commit is contained in:
Ilenia
2025-05-30 20:46:09 +02:00
committed by GitHub
parent 57642ef790
commit 4546af911d
4 changed files with 166 additions and 2 deletions

View File

@@ -3970,8 +3970,10 @@
"workshop-merge-sort": { "title": "Build a Merge Sort", "intro": [""] },
"lab-quick-sort": { "title": "Build a Quick Sort", "intro": [""] },
"lab-selection-sort": {
"title": "Build a Selection Sort",
"intro": [""]
"title": "Implement the Selection Sort Algorithm",
"intro": [
"In this lab you will implement the selection sort algorithm."
]
},
"lab-luhn-argorithm": {
"title": "Build a Luhn Algorithm",

View File

@@ -0,0 +1,9 @@
---
title: Introduction to the Implement the Selection Sort Algorithm
block: lab-selection-sort
superBlock: full-stack-developer
---
## Introduction to the Implement the Selection Sort Algorithm
In this lab you will implement the selection sort algorithm.

View File

@@ -0,0 +1,10 @@
{
"name": "Selection Sort Lab",
"isUpcomingChange": true,
"dashedName": "lab-selection-sort",
"superBlock": "full-stack-developer",
"challengeOrder": [{ "id": "680b3ef395479b0e449ecb6e", "title": "Build a Selection Sort" }],
"helpCategory": "Python",
"blockLayout": "link",
"blockType": "lab"
}

View File

@@ -0,0 +1,143 @@
---
id: 680b3ef395479b0e449ecb6e
title: Implement the Selection Sort Algorithm
challengeType: 27
dashedName: implement-selection-sort-algorithm
---
# --description--
Selection sort is another popular sorting algorithm taught in most computer science courses.
This algorithm works by repeatedly finding the smallest element from the unsorted portion of the list and swapping it with the first unsorted element. It begins by selecting the minimum value in the entire list and swapping it with the first element. Then it moves to the second position, finds the smallest value in the remaining unsorted elements, and swaps it with the second element. This process continues, moving through the list one element at a time, until the entire list is sorted.
Selection sort results in a quadratic time complexity in the best, average, and worst case scenarios. The space complexity will be constant `O(1)` because the sorting is done in place and a constant amount of memory is being used regardless of the size of the list.
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories**
1. You should define a function named `selection_sort`.
1. Your `selection_sort` function should have one parameter that represents the list of items.
1. Your `selection_sort` function should take a list and sort the items in place from smallest to largest.
1. Your `selection_sort` function should not use either the built-in `sort()` method or `sorted()` function.
# --hints--
You should have a function named `selection_sort`.
```js
({test: () => {
runPython(`
assert _Node(_code).has_function('selection_sort')
`)
}})
```
Your `selection_sort` function should have one parameter.
```js
(
{test: () => {
runPython(`
import inspect
sig = inspect.signature(selection_sort)
assert len(sig.parameters) == 1
`)
}}
)
```
You should not use the built-in `sort()` method or `sorted()` function in your code.
```js
(
{
test: () => runPython(`
assert not _Node(_code).block_has_call("sort")
assert not _Node(_code).block_has_call("sorted")
`)
}
)
```
`selection_sort([33, 1, 89, 2, 67, 245])` should return `[1, 2, 33, 89, 67, 245]`.
```js
(
{
test: () => runPython(`
assert selection_sort([33, 1, 89, 2, 67, 245]) == [1, 2, 33, 67, 89, 245]
`)
}
)
```
`selection_sort([5, 16, 99, 12, 567, 23, 15, 72, 3])` should return `[3, 5, 12, 15, 16, 23, 72, 99, 567]`.
```js
(
{
test: () => runPython(`
assert selection_sort([5, 16, 99, 12, 567, 23, 15, 72, 3]) == [3, 5, 12, 15, 16, 23, 72, 99, 567]
`)
}
)
```
`selection_sort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92])` should return `[1, 1, 2, 2, 4, 8, 32, 43, 43, 55, 63, 92, 123, 123, 234, 345, 5643]`.
```js
(
{
test: () => runPython(`
assert selection_sort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]) == [1, 1, 2, 2, 4, 8, 32, 43, 43, 55, 63, 92, 123, 123, 234, 345, 5643]
`)
}
)
```
Your `selection_sort` function should sort correctly any list of numbers.
```js
(
{
test: () => runPython(`
assert selection_sort([42, 17, 93, 8, 61, 29]) == [8, 17, 29, 42, 61, 93]
assert selection_sort([11, 4, 78, 23, 55, 198, 65, 90, 2]) == [2, 4, 11, 23, 55, 65, 78, 90, 198]
assert selection_sort([9, 27, 3, 7, 101, 66, 34, 52, 87, 42, 12, 29]) == [3, 7, 9, 12, 27, 29, 34, 42, 52, 66, 87, 101]
assert selection_sort([5, 14, 33, 77, 2, 18, 92, 1, 100, 45, 73, 64, 28, 56]) == [1, 2, 5, 14, 18, 28, 33, 45, 56, 64, 73, 77, 92, 100]
`)
}
)
```
# --seed--
## --seed-contents--
```py
```
# --solutions--
```py
def selection_sort(nums):
for i, _ in enumerate(nums):
min_index = i
for j in range(i + 1, len(nums)):
if nums[j] < nums[min_index]:
min_index = j
if min_index != i:
nums[i], nums[min_index] = nums[min_index], nums[i]
return nums
```