diff --git a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-bubble-sort.english.md b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-bubble-sort.english.md index c136106729..f68aac835c 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-bubble-sort.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-bubble-sort.english.md @@ -44,13 +44,11 @@ tests: ```js function bubbleSort(array) { // change code below this line - - // change code above this line return array; + // change code above this line } -// test array: -// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92] +bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]); ``` @@ -74,6 +72,23 @@ function isSorted(arr) {
```js -// solution required +function bubbleSort(array) { + for (let i = 0; i < array.length; i++) { + let swapped = false; + for (let j = 1; j < array.length; j++) { + if (array[j - 1] > array[j]) { + let temp = array[j-1]; + array[j-1] = array[j]; + array[j] = temp; + swapped = true; + } + } + if (swapped === false) { + break; + } + } + return array; +} ``` +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-insertion-sort.english.md b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-insertion-sort.english.md index 8a9af18c6c..6c6a81a4e7 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-insertion-sort.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-insertion-sort.english.md @@ -42,13 +42,11 @@ tests: ```js function insertionSort(array) { // change code below this line - - // change code above this line return array; + // change code above this line } -// test array: -// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92] +insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]); ``` @@ -72,6 +70,18 @@ function isSorted(arr) {
```js -// solution required +function insertionSort (array) { + for (let currentIndex = 0; currentIndex < array.length; currentIndex++) { + let current = array[currentIndex]; + let j = currentIndex - 1; + while (j > -1 && array[j] > current) { + array[j + 1] = array[j]; + j--; + } + array[j + 1] = current; + } + return array; +} ``` +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-merge-sort.english.md b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-merge-sort.english.md index 877bfea1c3..c697b750f6 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-merge-sort.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-merge-sort.english.md @@ -51,8 +51,7 @@ function mergeSort(array) { return array; } -// test array: -// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92] +mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]); ``` @@ -78,4 +77,5 @@ function isSorted(arr) { ```js // solution required ``` + diff --git a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-selection-sort.english.md b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-selection-sort.english.md index 1b7df4cfb8..3131413ded 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-selection-sort.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/algorithms/implement-selection-sort.english.md @@ -42,13 +42,12 @@ tests: ```js function selectionSort(array) { // change code below this line - - // change code above this line return array; + // change code above this line } -// test array: -// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92] + +selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]); ``` @@ -72,6 +71,20 @@ function isSorted(arr) {
```js -// solution required +function selectionSort(array) { + for (let i = 0; i < array.length-1; i++) { + let minimumIndex = i; + for (let j = i+1; j < array.length; j++){ + if (array[j] < array[minimumIndex]) { + minimumIndex = j; + } + } + let value = array[minimumIndex]; + array[minimumIndex] = array[i]; + array[i] = value; + } + return array; +} ``` +