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:
@ -44,13 +44,11 @@ tests:
|
|||||||
```js
|
```js
|
||||||
function bubbleSort(array) {
|
function bubbleSort(array) {
|
||||||
// change code below this line
|
// change code below this line
|
||||||
|
|
||||||
// change code above this line
|
|
||||||
return array;
|
return array;
|
||||||
|
// change code above this line
|
||||||
}
|
}
|
||||||
|
|
||||||
// test array:
|
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -74,6 +72,23 @@ function isSorted(arr) {
|
|||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```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>
|
</section>
|
||||||
|
@ -42,13 +42,11 @@ tests:
|
|||||||
```js
|
```js
|
||||||
function insertionSort(array) {
|
function insertionSort(array) {
|
||||||
// change code below this line
|
// change code below this line
|
||||||
|
|
||||||
// change code above this line
|
|
||||||
return array;
|
return array;
|
||||||
|
// change code above this line
|
||||||
}
|
}
|
||||||
|
|
||||||
// test array:
|
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -72,6 +70,18 @@ function isSorted(arr) {
|
|||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```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>
|
</section>
|
||||||
|
@ -51,8 +51,7 @@ function mergeSort(array) {
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
// test array:
|
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||||
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -78,4 +77,5 @@ function isSorted(arr) {
|
|||||||
```js
|
```js
|
||||||
// solution required
|
// solution required
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
@ -42,13 +42,12 @@ tests:
|
|||||||
```js
|
```js
|
||||||
function selectionSort(array) {
|
function selectionSort(array) {
|
||||||
// change code below this line
|
// change code below this line
|
||||||
|
|
||||||
// change code above this line
|
|
||||||
return array;
|
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>
|
</div>
|
||||||
@ -72,6 +71,20 @@ function isSorted(arr) {
|
|||||||
<section id='solution'>
|
<section id='solution'>
|
||||||
|
|
||||||
```js
|
```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>
|
</section>
|
||||||
|
Reference in New Issue
Block a user