Solutions for sorting algorithm challenges in Coding Interview Prep/Algorithms (#35905)

* added bubbleSort solution

* added insertionSort

* fixed spacing in bubbleSort

* added selectionSort

* added a test

* Fixed a reference to the array with the wrong array name in the solution

* fix: added extra lines to avoid linting errors

* fix: added comments back into seed
This commit is contained in:
Tina Wang
2019-06-20 11:03:31 -04:00
committed by Tom
parent ba96e82a01
commit f203273e3a
4 changed files with 55 additions and 17 deletions

View File

@ -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]);
```
</div>
@ -74,6 +72,23 @@ function isSorted(arr) {
<section id='solution'>
```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;
}
```
</section>

View File

@ -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]);
```
</div>
@ -72,6 +70,18 @@ function isSorted(arr) {
<section id='solution'>
```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;
}
```
</section>

View File

@ -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]);
```
</div>
@ -78,4 +77,5 @@ function isSorted(arr) {
```js
// solution required
```
</section>

View File

@ -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]);
```
</div>
@ -72,6 +71,20 @@ function isSorted(arr) {
<section id='solution'>
```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;
}
```
</section>