Skip to content

Commit 4546af9

Browse files
feat: add selection sort lab (freeCodeCamp#60591)
1 parent 57642ef commit 4546af9

4 files changed

Lines changed: 166 additions & 2 deletions

File tree

client/i18n/locales/english/intro.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3970,8 +3970,10 @@
39703970
"workshop-merge-sort": { "title": "Build a Merge Sort", "intro": [""] },
39713971
"lab-quick-sort": { "title": "Build a Quick Sort", "intro": [""] },
39723972
"lab-selection-sort": {
3973-
"title": "Build a Selection Sort",
3974-
"intro": [""]
3973+
"title": "Implement the Selection Sort Algorithm",
3974+
"intro": [
3975+
"In this lab you will implement the selection sort algorithm."
3976+
]
39753977
},
39763978
"lab-luhn-argorithm": {
39773979
"title": "Build a Luhn Algorithm",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Introduction to the Implement the Selection Sort Algorithm
3+
block: lab-selection-sort
4+
superBlock: full-stack-developer
5+
---
6+
7+
## Introduction to the Implement the Selection Sort Algorithm
8+
9+
In this lab you will implement the selection sort algorithm.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Selection Sort Lab",
3+
"isUpcomingChange": true,
4+
"dashedName": "lab-selection-sort",
5+
"superBlock": "full-stack-developer",
6+
"challengeOrder": [{ "id": "680b3ef395479b0e449ecb6e", "title": "Build a Selection Sort" }],
7+
"helpCategory": "Python",
8+
"blockLayout": "link",
9+
"blockType": "lab"
10+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
id: 680b3ef395479b0e449ecb6e
3+
title: Implement the Selection Sort Algorithm
4+
challengeType: 27
5+
dashedName: implement-selection-sort-algorithm
6+
---
7+
8+
# --description--
9+
10+
Selection sort is another popular sorting algorithm taught in most computer science courses.
11+
12+
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.
13+
14+
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.
15+
16+
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
17+
18+
**User Stories**
19+
20+
1. You should define a function named `selection_sort`.
21+
1. Your `selection_sort` function should have one parameter that represents the list of items.
22+
1. Your `selection_sort` function should take a list and sort the items in place from smallest to largest.
23+
1. Your `selection_sort` function should not use either the built-in `sort()` method or `sorted()` function.
24+
25+
# --hints--
26+
27+
You should have a function named `selection_sort`.
28+
29+
```js
30+
({test: () => {
31+
runPython(`
32+
assert _Node(_code).has_function('selection_sort')
33+
`)
34+
}})
35+
```
36+
37+
Your `selection_sort` function should have one parameter.
38+
39+
```js
40+
(
41+
{test: () => {
42+
runPython(`
43+
import inspect
44+
sig = inspect.signature(selection_sort)
45+
assert len(sig.parameters) == 1
46+
`)
47+
}}
48+
)
49+
```
50+
51+
You should not use the built-in `sort()` method or `sorted()` function in your code.
52+
53+
```js
54+
(
55+
{
56+
test: () => runPython(`
57+
assert not _Node(_code).block_has_call("sort")
58+
assert not _Node(_code).block_has_call("sorted")
59+
`)
60+
}
61+
)
62+
```
63+
64+
`selection_sort([33, 1, 89, 2, 67, 245])` should return `[1, 2, 33, 89, 67, 245]`.
65+
66+
```js
67+
(
68+
{
69+
test: () => runPython(`
70+
assert selection_sort([33, 1, 89, 2, 67, 245]) == [1, 2, 33, 67, 89, 245]
71+
72+
`)
73+
}
74+
)
75+
```
76+
77+
`selection_sort([5, 16, 99, 12, 567, 23, 15, 72, 3])` should return `[3, 5, 12, 15, 16, 23, 72, 99, 567]`.
78+
79+
```js
80+
(
81+
{
82+
test: () => runPython(`
83+
assert selection_sort([5, 16, 99, 12, 567, 23, 15, 72, 3]) == [3, 5, 12, 15, 16, 23, 72, 99, 567]
84+
85+
`)
86+
}
87+
)
88+
```
89+
90+
`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]`.
91+
92+
```js
93+
(
94+
{
95+
test: () => runPython(`
96+
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]
97+
98+
`)
99+
}
100+
)
101+
```
102+
103+
Your `selection_sort` function should sort correctly any list of numbers.
104+
105+
```js
106+
(
107+
{
108+
test: () => runPython(`
109+
assert selection_sort([42, 17, 93, 8, 61, 29]) == [8, 17, 29, 42, 61, 93]
110+
assert selection_sort([11, 4, 78, 23, 55, 198, 65, 90, 2]) == [2, 4, 11, 23, 55, 65, 78, 90, 198]
111+
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]
112+
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]
113+
114+
`)
115+
}
116+
)
117+
```
118+
119+
# --seed--
120+
121+
## --seed-contents--
122+
123+
```py
124+
125+
```
126+
127+
# --solutions--
128+
129+
```py
130+
def selection_sort(nums):
131+
for i, _ in enumerate(nums):
132+
min_index = i
133+
134+
for j in range(i + 1, len(nums)):
135+
if nums[j] < nums[min_index]:
136+
min_index = j
137+
138+
if min_index != i:
139+
nums[i], nums[min_index] = nums[min_index], nums[i]
140+
141+
return nums
142+
143+
```

0 commit comments

Comments
 (0)