Skip to content

Commit c397879

Browse files
feat(curriculum): add insertion sort lab to jsv9 (freeCodeCamp#65872)
Co-authored-by: Jeevankumar S <110320697+Jeevankumar-s@users.noreply.github.com>
1 parent 5177bce commit c397879

4 files changed

Lines changed: 114 additions & 0 deletions

File tree

client/i18n/locales/english/intro.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,6 +3231,12 @@
32313231
"In this lab you will implement the selection sort algorithm."
32323232
]
32333233
},
3234+
"lab-insertion-sort": {
3235+
"title": "Implement the Insertion Sort Algorithm",
3236+
"intro": [
3237+
"In this lab, you will implement the insertion sort algorithm to sort an array of integers in ascending order."
3238+
]
3239+
},
32343240
"lab-quicksort-js": {
32353241
"title": "Implement the Quicksort Algorithm",
32363242
"intro": [
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
id: 587d8259367417b2b2512c86
3+
title: Implement Insertion Sort
4+
challengeType: 26
5+
dashedName: implement-insertion-sort
6+
---
7+
8+
# --description--
9+
10+
In this lab, you will implement the insertion sort algorithm. This method works by building up a sorted array at the beginning of the list. It begins the sorted array with the first element. Then it inspects the next element and swaps it backwards into the sorted array until it is in sorted position. It continues iterating through the list and swapping new items backwards into the sorted portion until it reaches the end. This algorithm has quadratic time complexity in the average and worst cases.
11+
12+
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
13+
14+
**User Stories:**
15+
16+
1. You should create an `insertionSort` function.
17+
1. The `insertionSort` function should take an array of integers and return an array with the same integers sorted from least to greatest.
18+
19+
# --hints--
20+
21+
`insertionSort` should be a function.
22+
23+
```js
24+
assert.isFunction(insertionSort);
25+
```
26+
27+
`insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92])` should return an array that is unchanged except for order.
28+
29+
```js
30+
assert.sameMembers(
31+
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]),
32+
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
33+
);
34+
```
35+
36+
`insertionSort` should return a sorted array (least to greatest).
37+
38+
```js
39+
40+
assert.sameOrderedMembers(
41+
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]),
42+
[1, 1, 2, 2, 4, 8, 32, 43, 43, 55, 63, 92, 123, 123, 234, 345, 5643]
43+
);
44+
```
45+
46+
`insertionSort([5, 4, 33, 2, 8])` should return `[2, 4, 5, 8, 33]`.
47+
48+
```js
49+
assert.sameOrderedMembers(insertionSort([5, 4, 33, 2, 8]), [2, 4, 5, 8, 33])
50+
```
51+
52+
`insertionSort` should not use the built-in `.sort()` method.
53+
54+
```js
55+
function isBuiltInSortUsed(){
56+
let sortUsed = false;
57+
const temp = Array.prototype.sort;
58+
Array.prototype.sort = () => sortUsed = true;
59+
try {
60+
insertionSort([0, 1]);
61+
} finally {
62+
Array.prototype.sort = temp;
63+
}
64+
return sortUsed;
65+
}
66+
assert.isFalse(isBuiltInSortUsed());
67+
```
68+
69+
# --seed--
70+
71+
## --seed-contents--
72+
73+
```js
74+
75+
```
76+
77+
# --solutions--
78+
79+
```js
80+
function insertionSort (array) {
81+
for (let currentIndex = 0; currentIndex < array.length; currentIndex++) {
82+
let current = array[currentIndex];
83+
let j = currentIndex - 1;
84+
while (j > -1 && array[j] > current) {
85+
array[j + 1] = array[j];
86+
j--;
87+
}
88+
array[j + 1] = current;
89+
}
90+
return array;
91+
}
92+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Implement the Insertion Sort Algorithm",
3+
"isUpcomingChange": true,
4+
"dashedName": "lab-insertion-sort",
5+
"helpCategory": "JavaScript",
6+
"blockLayout": "link",
7+
"challengeOrder": [
8+
{
9+
"id": "587d8259367417b2b2512c86",
10+
"title": "Implement the Insertion Sort Algorithm"
11+
}
12+
],
13+
"blockLabel": "lab",
14+
"usesMultifileEditor": true
15+
}

curriculum/structure/superblocks/javascript-v9.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@
303303
"lecture-introduction-to-common-searching-and-sorting-algorithms",
304304
"lab-bubble-sort-algorithm",
305305
"lab-selection-sort-js",
306+
"lab-insertion-sort",
306307
"lab-quicksort-js"
307308
]
308309
},

0 commit comments

Comments
 (0)