feat(interview-prep): Adding Rosetta Code problems (#34634)
This commit is contained in:
committed by
Kristofer Koishigawa
parent
0afe892563
commit
4d55a04e21
@ -332,6 +332,70 @@
|
||||
"5a23c84252665b21eecc8002",
|
||||
"Sorting algorithms/Bogosort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8003",
|
||||
"Sorting algorithms/Bubble sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8004",
|
||||
"Sorting algorithms/Cocktail sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8005",
|
||||
"Sorting algorithms/Comb sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8006",
|
||||
"Sorting algorithms/Counting sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8007",
|
||||
"Sorting algorithms/Gnome sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8008",
|
||||
"Sorting algorithms/Heapsort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8009",
|
||||
"Sorting algorithms/Insertion sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc800a",
|
||||
"Sorting algorithms/Merge sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc800b",
|
||||
"Sorting algorithms/Pancake sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc800c",
|
||||
"Sorting algorithms/Permutation sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc800d",
|
||||
"Sorting algorithms/Quicksort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc800e",
|
||||
"Sorting algorithms/Radix sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc800f",
|
||||
"Sorting algorithms/Selection sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8010",
|
||||
"Sorting algorithms/Shell sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8012",
|
||||
"Sorting algorithms/Stooge sort"
|
||||
],
|
||||
[
|
||||
"5a23c84252665b21eecc8013",
|
||||
"Sorting algorithms/Strand sort"
|
||||
],
|
||||
[
|
||||
"594ecc0d9a8cf816e3340187",
|
||||
"Taxicab numbers"
|
||||
|
@ -0,0 +1,87 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8003
|
||||
title: Sorting algorithms/Bubble sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type.
|
||||
The bubble sort is generally considered to be the simplest sorting algorithm.
|
||||
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
|
||||
Because of its abysmal O(n<sup>2</sup>) performance, it is not used often for large (or even medium-sized) datasets.
|
||||
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them). Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass. A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
|
||||
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
|
||||
<pre>
|
||||
repeat
|
||||
hasChanged := false
|
||||
<b>decrement</b> itemCount
|
||||
<b>repeat with</b> index <b>from</b> 1 <b>to</b> itemCount
|
||||
<b>if</b> (item <b>at</b> index) > (item <b>at</b> (index + 1))
|
||||
swap (item <b>at</b> index) with (item <b>at</b> (index + 1))
|
||||
hasChanged := true
|
||||
<b>until</b> hasChanged = <b>false</b>
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>bubblesort</code> should be a function.
|
||||
testString: assert(typeof bubblesort == 'function', '<code>bubblesort</code> should be a function.');
|
||||
- text: <code>bubblesort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(bubblesort([25, 32, 12, 7, 20])), '<code>bubblesort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>bubblesort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(bubblesort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>bubblesort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>bubblesort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(bubblesort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>bubblesort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>bubblesort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(bubblesort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>bubblesort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>bubblesort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(bubblesort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>bubblesort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>bubblesort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(bubblesort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>bubblesort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function bubblesort(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function bubblesort (arr) {
|
||||
var done = false;
|
||||
while (!done) {
|
||||
done = true;
|
||||
for (var i = 1; i<arr.length; i++) {
|
||||
if (arr[i-1] > arr[i]) {
|
||||
done = false;
|
||||
[arr[i-1], arr[i]] = [arr[i], arr[i-1]]
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,111 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8004
|
||||
title: Sorting algorithms/Cocktail sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The cocktail shaker sort is an improvement on the <a href="http://rosettacode.org/wiki/Bubble Sort">Bubble Sort</a>. The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from <a href="https://en.wikipedia.org/wiki/Cocktail sort">wikipedia</a>):</p>
|
||||
<pre>
|
||||
<b>function</b> <i>cocktailSort</i>( A : list of sortable items )
|
||||
<b>do</b>
|
||||
swapped := false
|
||||
<b>for each</b> i <b>in</b> 0 <b>to</b> length( A ) - 2 <b>do</b>
|
||||
<b>if</b> A[ i ] > A[ i+1 ] <b>then</b> <i>// test whether the two</i>
|
||||
<i>// elements are in the wrong</i>
|
||||
<i>// order</i>
|
||||
swap( A[ i ], A[ i+1 ] ) <i>// let the two elements</i>
|
||||
<i>// change places</i>
|
||||
swapped := true;
|
||||
<b>if</b> swapped = false <b>then</b>
|
||||
<i>// we can exit the outer loop here if no swaps occurred.</i>
|
||||
<b>break do-while loop</b>;
|
||||
swapped := false
|
||||
<b>for each</b> i <b>in</b> length( A ) - 2 <b>down to</b> 0 <b>do</b>
|
||||
<b>if</b> A[ i ] > A[ i+1 ] <b>then</b>
|
||||
swap( A[ i ], A[ i+1 ] )
|
||||
swapped := true;
|
||||
<b>while</b> swapped; <i>// if no elements have been swapped,</i>
|
||||
<i>// then the list is sorted</i>
|
||||
</pre>
|
||||
Write a function that sorts a given array using cocktail sort.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>cocktailSort</code> should be a function.
|
||||
testString: assert(typeof cocktailSort == 'function', '<code>cocktailSort</code> should be a function.');
|
||||
- text: <code>cocktailSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(cocktailSort([25, 32, 12, 7, 20])), '<code>cocktailSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>cocktailSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(cocktailSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>cocktailSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>cocktailSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(cocktailSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>cocktailSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>cocktailSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(cocktailSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>cocktailSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>cocktailSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(cocktailSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>cocktailSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>cocktailSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(cocktailSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>cocktailSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function cocktailSort (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function cocktailSort (arr) {
|
||||
let isSorted = true;
|
||||
while (isSorted) {
|
||||
for (let i = 0; i < arr.length - 1; i++) {
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
let temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
isSorted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isSorted)
|
||||
break;
|
||||
|
||||
isSorted = false;
|
||||
|
||||
for (let j = arr.length - 1; j > 0; j--) {
|
||||
if (arr[j - 1] > arr[j]) {
|
||||
let temp = arr[j];
|
||||
arr[j] = arr[j - 1];
|
||||
arr[j - 1] = temp;
|
||||
isSorted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,140 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8005
|
||||
title: Sorting algorithms/Comb sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Implement a <i>comb sort</i>.
|
||||
The <b>Comb Sort</b> is a variant of the <a href="http://rosettacode.org/wiki/Bubble Sort">Bubble Sort</a>.
|
||||
Like the <a href="http://rosettacode.org/wiki/Shell sort">Shell sort</a>, the Comb Sort increases the gap used in comparisons and exchanges.
|
||||
Dividing the gap by $(1-e^{-\varphi})^{-1} \approx 1.247330950103979$ works best, but 1.3 may be more practical.
|
||||
Some implementations use the insertion sort once the gap is less than a certain amount.
|
||||
<b>Also see</b>
|
||||
<ul>
|
||||
<li>the Wikipedia article: <a href="https://en.wikipedia.org/wiki/Comb sort">Comb sort</a>.</li>
|
||||
</ul>
|
||||
Variants:
|
||||
<ul>
|
||||
<li>Combsort11 makes sure the gap ends in (11, 8, 6, 4, 3, 2, 1), which is significantly faster than the other two possible endings.</li>
|
||||
<li>Combsort with different endings changes to a more efficient sort when the data is almost sorted (when the gap is small). Comb sort with a low gap isn't much better than the Bubble Sort.</li>
|
||||
</ul>
|
||||
Pseudocode:
|
||||
<pre>
|
||||
<b>function</b> combsort(<b>array</b> input)
|
||||
gap := input<b>.size</b> <i>//initialize gap size</i>
|
||||
<b>loop until</b> gap = 1 <b>and</b> swaps = 0
|
||||
<i>//update the gap value for a next comb. Below is an example</i>
|
||||
gap := int(gap / 1.25)
|
||||
<b>if</b> gap < 1
|
||||
<i>//minimum gap is 1</i>
|
||||
gap := 1
|
||||
<b>end if</b>
|
||||
i := 0
|
||||
swaps := 0 <i>//see <a href="http://rosettacode.org/wiki/Bubble Sort">Bubble Sort</a> for an explanation</i>
|
||||
<i>//a single "comb" over the input list</i>
|
||||
<b>loop until</b> i + gap >= input<b>.size</b> <i>//see <a href="http://rosettacode.org/wiki/Shell sort">Shell sort</a> for similar idea</i>
|
||||
<b>if</b> input[i] > input[i+gap]
|
||||
<b>swap</b>(input[i], input[i+gap])
|
||||
swaps := 1 <i>// Flag a swap has occurred, so the</i>
|
||||
<i>// list is not guaranteed sorted</i>
|
||||
<b>end if</b>
|
||||
i := i + 1
|
||||
<b>end loop</b>
|
||||
<b>end loop</b>
|
||||
<b>end function</b>
|
||||
</pre>
|
||||
Write a function that sorts a given array using Comb sort.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>combSort</code> should be a function.
|
||||
testString: assert(typeof combSort == 'function', '<code>combSort</code> should be a function.');
|
||||
- text: <code>combSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(combSort([25, 32, 12, 7, 20])), '<code>combSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>combSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(combSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>combSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>combSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(combSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>combSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>combSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(combSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>combSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>combSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(combSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>combSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>combSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(combSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>combSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function combSort (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function combSort (arr) {
|
||||
function is_array_sorted(arr) {
|
||||
var sorted = true;
|
||||
for (var i = 0; i < arr.length - 1; i++) {
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
sorted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
var iteration_count = 0;
|
||||
var gap = arr.length - 2;
|
||||
var decrease_factor = 1.25;
|
||||
|
||||
// Until array is not sorted, repeat iterations
|
||||
while (!is_array_sorted(arr)) {
|
||||
// If not first gap
|
||||
if (iteration_count > 0)
|
||||
// Calculate gap
|
||||
gap = (gap == 1) ? gap : Math.floor(gap / decrease_factor);
|
||||
|
||||
// Set front and back elements and increment to a gap
|
||||
var front = 0;
|
||||
var back = gap;
|
||||
while (back <= arr.length - 1) {
|
||||
// If elements are not ordered swap them
|
||||
if (arr[front] > arr[back]) {
|
||||
var temp = arr[front];
|
||||
arr[front] = arr[back];
|
||||
arr[back] = temp;
|
||||
}
|
||||
|
||||
// Increment and re-run swapping
|
||||
front += 1;
|
||||
back += 1;
|
||||
}
|
||||
iteration_count += 1;
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8006
|
||||
title: Sorting algorithms/Counting sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Implement the <a href="https://en.wikipedia.org/wiki/Counting sort">Counting sort</a>. This is a way of sorting integers when the minimum and maximum value are known.
|
||||
Pseudocode:
|
||||
<pre>
|
||||
<b>function</b> <i>countingSort</i>(array, min, max):
|
||||
count: <b>array of</b> (max - min + 1) <b>elements</b>
|
||||
<b>initialize</b> count <b>with</b> 0
|
||||
<b>for each</b> number <b>in</b> array <b>do</b>
|
||||
count[number - min] := count[number - min] + 1
|
||||
<b>done</b>
|
||||
z := 0
|
||||
<b>for</b> i <b>from</b> min <b>to</b> max <b>do</b>
|
||||
<b>while</b> ( count[i - min] > 0 ) <b>do</b>
|
||||
array[z] := i
|
||||
z := z+1
|
||||
count[i - min] := count[i - min] - 1
|
||||
<b>done</b>
|
||||
<b>done</b>
|
||||
</pre>
|
||||
Write a function to implement the above pseudo code. The function should return the sorted array.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>countSort</code> should be a function.
|
||||
testString: assert(typeof countSort == 'function', '<code>countSort</code> should be a function.');
|
||||
- text: <code>countSort([25, 32, 12, 7, 20],7, 32)</code> should return a array.
|
||||
testString: assert(Array.isArray(countSort([25, 32, 12, 7, 20], 7, 32)), '<code>countSort([25, 32, 12, 7, 20],7, 32)</code> should return a array.');
|
||||
- text: <code>countSort([25, 32, 12, 7, 20],7, 32)</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(countSort([25, 32, 12, 7, 20], 7, 32), [7, 12, 20, 25, 32], '<code>countSort([25, 32, 12, 7, 20],7, 32)</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>countSort([38, 45, 35, 8, 13],8, 45)</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(countSort([38, 45, 35, 8, 13], 8, 45), [8, 13, 35, 38, 45], '<code>countSort([38, 45, 35, 8, 13],8, 45)</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>countSort([43, 36, 20, 34, 24],20, 43)</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(countSort([43, 36, 20, 34, 24], 20, 43), [20, 24, 34, 36, 43], '<code>countSort([43, 36, 20, 34, 24],20, 43)</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>countSort([12, 33, 26, 18, 1, 16, 38],1, 38)</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(countSort([12, 33, 26, 18, 1, 16, 38], 1, 38), [1, 12, 16, 18, 26, 33, 38], '<code>countSort([12, 33, 26, 18, 1, 16, 38],1, 38)</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>countSort([3, 39, 48, 16, 1, 4, 29],1, 48)</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(countSort([3, 39, 48, 16, 1, 4, 29], 1, 48), [1, 3, 4, 16, 29, 39, 48], '<code>countSort([3, 39, 48, 16, 1, 4, 29],1, 48)</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function countSort (arr, min, max) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function countSort (arr, min, max) {
|
||||
var i, z = 0,
|
||||
count = [];
|
||||
|
||||
for (i = min; i <= max; i++) {
|
||||
count[i] = 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < arr.length; i++) {
|
||||
count[arr[i]]++;
|
||||
}
|
||||
|
||||
for (i = min; i <= max; i++) {
|
||||
while (count[i]-- > 0) {
|
||||
arr[z++] = i;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8007
|
||||
title: Sorting algorithms/Gnome sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Gnome sort is a sorting algorithm which is similar to <a href="http://rosettacode.org/wiki/Insertion sort">Insertion sort</a>, except that moving an element to its proper place is accomplished by a series of swaps, as in <a href="http://rosettacode.org/wiki/Bubble Sort">Bubble Sort</a>.
|
||||
The pseudocode for the algorithm is:
|
||||
<pre>
|
||||
<b>function</b> <i>gnomeSort</i>(a[0..size-1])
|
||||
i := 1
|
||||
j := 2
|
||||
<b>while</b> i < size <b>do</b>
|
||||
<b>if</b> a[i-1] <= a[i] <b>then</b>
|
||||
<i>/// for descending sort, use >= for comparison</i>
|
||||
i := j
|
||||
j := j + 1
|
||||
<b>else</b>
|
||||
<b>swap</b> a[i-1] <b>and</b> a[i]
|
||||
i := i - 1
|
||||
<b>if</b> i = 0 <b>then</b>
|
||||
i := j
|
||||
j := j + 1
|
||||
<b>endif</b>
|
||||
<b>endif</b>
|
||||
<b>done</b>
|
||||
</pre>
|
||||
Write a function to implement the above pseudo code. The function should return the sorted array.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>gnomeSort</code> should be a function.
|
||||
testString: assert(typeof gnomeSort == 'function', '<code>gnomeSort</code> should be a function.');
|
||||
- text: <code>gnomeSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(gnomeSort([25, 32, 12, 7, 20])), '<code>gnomeSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>gnomeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(gnomeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>gnomeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>gnomeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(gnomeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>gnomeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>gnomeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(gnomeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>gnomeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>gnomeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(gnomeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>gnomeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>gnomeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(gnomeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>gnomeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function gnomeSort(a) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function gnomeSort(a) {
|
||||
function moveBack(i) {
|
||||
for (; i > 0 && a[i - 1] > a[i]; i--) {
|
||||
var t = a[i];
|
||||
a[i] = a[i - 1];
|
||||
a[i - 1] = t;
|
||||
}
|
||||
}
|
||||
for (var i = 1; i < a.length; i++) {
|
||||
if (a[i - 1] > a[i]) moveBack(i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,146 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8008
|
||||
title: Sorting algorithms/Heapsort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<a href="https://en.wikipedia.org/wiki/Heapsort">Heapsort</a> is an in-place sorting algorithm with worst case and average complexity of <span style="font-family: serif">O(<i>n</i> log<i>n</i>)</span>.
|
||||
The basic idea is to turn the array into a binary heap structure, which has the property that it allows efficient retrieval and removal of the maximal element.
|
||||
We repeatedly "remove" the maximal element from the heap, thus building the sorted list from back to front.
|
||||
Heapsort requires random access, so can only be used on an array-like data structure.
|
||||
Pseudocode:
|
||||
<pre>
|
||||
<b>function</b> heapSort(a, count) <b>is</b>
|
||||
<b>input:</b> an unordered array <i>a</i> of length <i>count</i><br>
|
||||
<span style="color: grey"><i>(first place a in max-heap order)</i></span>
|
||||
heapify(a, count)<br>
|
||||
end := count - 1
|
||||
<b>while</b> end > 0 <b>do</b>
|
||||
<span style="color: grey"><i>(swap the root(maximum value) of the heap with the</i>
|
||||
<i>last element of the heap)</i></span>
|
||||
swap(a[end], a[0])
|
||||
<span style="color: grey"><i>(decrement the size of the heap so that the previous</i>
|
||||
<i>max value will stay in its proper place)</i></span>
|
||||
end := end - 1
|
||||
<span style="color: grey"><i>(put the heap back in max-heap order)</i></span>
|
||||
siftDown(a, 0, end)
|
||||
</pre>
|
||||
<pre>
|
||||
<b>function</b> heapify(a,count) <b>is</b>
|
||||
<span style="color: grey"><i>(start is assigned the index in </i>a<i> of the last parent node)</i></span>
|
||||
start := (count - 2) / 2<br>
|
||||
<b>while</b> start ≥ 0 <b>do</b>
|
||||
<span style="color: grey"><i>(sift down the node at index start to the proper place</i>
|
||||
<i>such that all nodes below the start index are in heap</i>
|
||||
<i>order)</i></span>
|
||||
siftDown(a, start, count-1)
|
||||
start := start - 1
|
||||
<span style="color: grey"><i>(after sifting down the root all nodes/elements are in heap order)</i></span><br>
|
||||
<b>function</b> siftDown(a, start, end) <b>is</b>
|
||||
<span style="color: grey"><i>(</i>end<i> represents the limit of how far down the heap to sift)</i></span>
|
||||
root := start<br>
|
||||
<b>while</b> root * 2 + 1 ≤ end <b>do</b> <span style="color: grey"><i>(While the root has at least one child)</i></span>
|
||||
child := root * 2 + 1 <span style="color: grey"><i>(root*2+1 points to the left child)</i></span>
|
||||
<span style="color: grey"><i>(If the child has a sibling and the child's value is less than its sibling's...)</i></span>
|
||||
<b>if</b> child + 1 ≤ end <b>and</b> a[child] < a[child + 1] <b>then</b>
|
||||
child := child + 1 <span style="color: grey"><i>(... then point to the right child instead)</i></span>
|
||||
<b>if</b> a[root] < a[child] <b>then</b> <span style="color: grey"><i>(out of max-heap order)</i></span>
|
||||
swap(a[root], a[child])
|
||||
root := child <span style="color: grey"><i>(repeat to continue sifting down the child now)</i></span>
|
||||
<b>else</b>
|
||||
<b>return</b>
|
||||
</pre>
|
||||
Write a function to sort a collection of integers using heapsort.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>heapSort</code> should be a function.
|
||||
testString: assert(typeof heapSort == 'function', '<code>heapSort</code> should be a function.');
|
||||
- text: <code>heapSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(heapSort([25, 32, 12, 7, 20])), '<code>heapSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>heapSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(heapSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>heapSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>heapSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(heapSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>heapSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>heapSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(heapSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>heapSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>heapSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(heapSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>heapSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>heapSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(heapSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>heapSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function heapSort (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function heapSort (arr) {
|
||||
function swap(data, i, j) {
|
||||
var tmp = data[i];
|
||||
data[i] = data[j];
|
||||
data[j] = tmp;
|
||||
}
|
||||
|
||||
function put_array_in_heap_order(arr) {
|
||||
var i;
|
||||
i = arr.length / 2 - 1;
|
||||
i = Math.floor(i);
|
||||
while (i >= 0) {
|
||||
sift_element_down_heap(arr, i, arr.length);
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
function sift_element_down_heap(heap, i, max) {
|
||||
var i_big, c1, c2;
|
||||
while (i < max) {
|
||||
i_big = i;
|
||||
c1 = 2 * i + 1;
|
||||
c2 = c1 + 1;
|
||||
if (c1 < max && heap[c1] > heap[i_big])
|
||||
i_big = c1;
|
||||
if (c2 < max && heap[c2] > heap[i_big])
|
||||
i_big = c2;
|
||||
if (i_big == i) return;
|
||||
swap(heap, i, i_big);
|
||||
i = i_big;
|
||||
}
|
||||
}
|
||||
put_array_in_heap_order(arr);
|
||||
var end = arr.length - 1;
|
||||
while (end > 0) {
|
||||
swap(arr, 0, end);
|
||||
sift_element_down_heap(arr, 0, end);
|
||||
end -= 1
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8009
|
||||
title: Sorting algorithms/Insertion sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
An <span style="font-family: serif"><a href="http://rosettacode.org/wiki/O">O</a>(<i>n</i><sup>2</sup>)</span> sorting algorithm which moves elements one at a time into the correct position. The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part.
|
||||
Although insertion sort is an <span style="font-family: serif"><a href="http://rosettacode.org/wiki/O">O</a>(<i>n</i><sup>2</sup>)</span> algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases:
|
||||
(i) small <span style="font-family: serif"><i>n</i></span>,
|
||||
(ii) as the final finishing-off algorithm for <span style="font-family: serif"><a href="http://rosettacode.org/wiki/O">O</a>(<i>n</i> log<i>n</i>)</span> algorithms such as <a href="http://rosettacode.org/wiki/Merge sort">mergesort</a> and <a href="http://rosettacode.org/wiki/quicksort">quicksort</a>.
|
||||
The algorithm is as follows (from <a href="https://en.wikipedia.org/wiki/Insertion_sort#Algorithm">wikipedia</a>):
|
||||
<pre>
|
||||
<b>function</b> <i>insertionSort</i>(array A)
|
||||
<b>for</b> i <b>from</b> 1 <b>to</b> length[A]-1 <b>do</b>
|
||||
value := A[i]
|
||||
j := i-1
|
||||
<b>while</b> j >= 0 <b>and</b> A[j] > value <b>do</b>
|
||||
A[j+1] := A[j]
|
||||
j := j-1
|
||||
<b>done</b>
|
||||
A[j+1] = value
|
||||
<b>done</b>
|
||||
</pre>
|
||||
Write a function that performs insertion sort on a given array. The function should return the sorted array.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>insertionSort</code> should be a function.
|
||||
testString: assert(typeof insertionSort == 'function', '<code>insertionSort</code> should be a function.');
|
||||
- text: <code>insertionSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(insertionSort([25, 32, 12, 7, 20])), '<code>insertionSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>insertionSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(insertionSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>insertionSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>insertionSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(insertionSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>insertionSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>insertionSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(insertionSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>insertionSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>insertionSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(insertionSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>insertionSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>insertionSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(insertionSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>insertionSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function insertionSort (a) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function insertionSort (a) {
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
var k = a[i];
|
||||
for (var j = i; j > 0 && k < a[j - 1]; j--)
|
||||
a[j] = a[j - 1];
|
||||
a[j] = k;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,132 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc800a
|
||||
title: Sorting algorithms/Merge sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <b>merge sort</b> is a recursive sort of order n*log(n).
|
||||
It is notable for having a worst case and average complexity of <i>O(n*log(n))</i>, and a best case complexity of <i>O(n)</i> (for pre-sorted input).
|
||||
The basic idea is to split the collection into smaller groups by halving it until the groups only have one element or no elements (which are both entirely sorted groups).
|
||||
Then merge the groups back together so that their elements are in order.
|
||||
This is how the algorithm gets its <i>divide and conquer</i> description.
|
||||
Write a function to sort a collection of integers using the merge sort. The function should return the sorted array.
|
||||
The merge sort algorithm comes in two parts: a sort function and a merge function
|
||||
The functions in pseudocode look like this:
|
||||
<pre>
|
||||
<b>function</b> <i>mergesort</i>(m)
|
||||
<b>var</b> list left, right, result
|
||||
<b>if</b> length(m) ≤ 1
|
||||
<b>return</b> m
|
||||
<b>else</b>
|
||||
<b>var</b> middle = length(m) / 2
|
||||
<b>for each</b> x <b>in</b> m <b>up to</b> middle - 1
|
||||
<b>add</b> x <b>to</b> left
|
||||
<b>for each</b> x <b>in</b> m <b>at and after</b> middle
|
||||
<b>add</b> x <b>to</b> right
|
||||
left = mergesort(left)
|
||||
right = mergesort(right)
|
||||
<b>if</b> last(left) ≤ first(right)
|
||||
<b>append</b> right <b>to</b> left
|
||||
<b>return</b> left
|
||||
result = merge(left, right)
|
||||
<b>return</b> result<br>
|
||||
<b>function</b> <i>merge</i>(left,right)
|
||||
<b>var</b> list result
|
||||
<b>while</b> length(left) > 0 and length(right) > 0
|
||||
<b>if</b> first(left) ≤ first(right)
|
||||
<b>append</b> first(left) <b>to</b> result
|
||||
left = rest(left)
|
||||
<b>else</b>
|
||||
<b>append</b> first(right) <b>to</b> result
|
||||
right = rest(right)
|
||||
<b>if</b> length(left) > 0
|
||||
<b>append</b> rest(left) <b>to</b> result
|
||||
<b>if</b> length(right) > 0
|
||||
<b>append</b> rest(right) <b>to</b> result
|
||||
<b>return</b> result
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>mergeSort</code> should be a function.
|
||||
testString: assert(typeof mergeSort == 'function', '<code>mergeSort</code> should be a function.');
|
||||
- text: <code>mergeSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(mergeSort([25, 32, 12, 7, 20])), '<code>mergeSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>mergeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(mergeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>mergeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>mergeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(mergeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>mergeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>mergeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(mergeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>mergeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>mergeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(mergeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>mergeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>mergeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(mergeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>mergeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function mergeSort (array) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function mergeSort (array) {
|
||||
function merge(left, right, arr) {
|
||||
var a = 0;
|
||||
|
||||
while (left.length && right.length) {
|
||||
arr[a++] = (right[0] < left[0]) ? right.shift() : left.shift();
|
||||
}
|
||||
while (left.length) {
|
||||
arr[a++] = left.shift();
|
||||
}
|
||||
while (right.length) {
|
||||
arr[a++] = right.shift();
|
||||
}
|
||||
}
|
||||
|
||||
function mergeSortRecurse(arr) {
|
||||
var len = arr.length;
|
||||
|
||||
if (len === 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
var mid = Math.floor(len / 2),
|
||||
left = arr.slice(0, mid),
|
||||
right = arr.slice(mid);
|
||||
|
||||
mergeSortRecurse(left);
|
||||
mergeSortRecurse(right);
|
||||
merge(left, right, arr);
|
||||
}
|
||||
mergeSortRecurse(array)
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc800b
|
||||
title: Sorting algorithms/Pancake sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to sort an array of integers (of any convenient size) into ascending order using <a href="https://en.wikipedia.org/wiki/Pancake sorting">Pancake sorting</a>. The function should return the sorted array.
|
||||
In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so:
|
||||
<pre>
|
||||
Before:
|
||||
<b>6 7 8 9</b> 2 5 3 4 1
|
||||
After:
|
||||
<b>9 8 7 6</b> 2 5 3 4 1
|
||||
</pre>
|
||||
Only one end of the list can be flipped; this should be the low end, but the high end is okay if it's easier to code or works better, but it <b>must</b> be the same end for the entire solution. (The end flipped can't be arbitrarily changed.)
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>pancakeSort</code> should be a function.
|
||||
testString: assert(typeof pancakeSort == 'function', '<code>pancakeSort</code> should be a function.');
|
||||
- text: <code>pancakeSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(pancakeSort([25, 32, 12, 7, 20])), '<code>pancakeSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>pancakeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(pancakeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>pancakeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>pancakeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(pancakeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>pancakeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>pancakeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(pancakeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>pancakeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>pancakeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(pancakeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>pancakeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>pancakeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(pancakeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>pancakeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function pancakeSort (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function pancakeSort(arr) {
|
||||
for (var i = arr.length - 1; i >= 1; i--) {
|
||||
// find the index of the largest element not yet sorted
|
||||
var max_idx = 0;
|
||||
var max = arr[0];
|
||||
for (var j = 1; j <= i; j++) {
|
||||
if (arr[j] > max) {
|
||||
max = arr[j];
|
||||
max_idx = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (max_idx == i)
|
||||
continue; // element already in place
|
||||
|
||||
var new_slice;
|
||||
|
||||
// flip arr max element to index 0
|
||||
if (max_idx > 0) {
|
||||
new_slice = arr.slice(0, max_idx + 1).reverse();
|
||||
for (var j = 0; j <= max_idx; j++)
|
||||
arr[j] = new_slice[j];
|
||||
}
|
||||
|
||||
// then flip the max element to its place
|
||||
new_slice = arr.slice(0, i + 1).reverse();
|
||||
for (var j = 0; j <= i; j++)
|
||||
arr[j] = new_slice[j];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,104 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc800c
|
||||
title: Sorting algorithms/Permutation sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to implement a permutation sort, which proceeds by generating the possible permutations of the input array until discovering the sorted one. The function should return the sorted array.
|
||||
Pseudocode:
|
||||
<pre>
|
||||
<b>while not</b> InOrder(list) <b>do</b>
|
||||
nextPermutation(list)
|
||||
<b>done</b>
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>permutationSort</code> should be a function.
|
||||
testString: assert(typeof permutationSort == 'function', '<code>permutationSort</code> should be a function.');
|
||||
- text: <code>permutationSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(permutationSort([25, 32, 12, 7, 20])), '<code>permutationSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>permutationSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(permutationSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>permutationSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>permutationSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(permutationSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>permutationSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>permutationSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(permutationSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>permutationSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>permutationSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(permutationSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>permutationSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>permutationSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(permutationSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>permutationSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function permutationSort (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function permutationSort (arr) {
|
||||
function pSort(a) {
|
||||
var list = [];
|
||||
permute(a, a.length, list);
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var x = list[i];
|
||||
if (isSorted(x))
|
||||
return x;
|
||||
}
|
||||
return a;
|
||||
};
|
||||
|
||||
function permute(a, n, list) {
|
||||
if (n === 1) {
|
||||
var b = a.slice();
|
||||
list.push(b);
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < n; i++) {
|
||||
swap(a, i, n - 1);
|
||||
permute(a, n - 1, list);
|
||||
swap(a, i, n - 1);
|
||||
};
|
||||
};
|
||||
|
||||
function isSorted(a) {
|
||||
for (var i = 1; i < a.length; i++)
|
||||
if (a[i - 1] > a[i])
|
||||
return false;;
|
||||
return true;
|
||||
};
|
||||
|
||||
function swap(arr, i, j) {
|
||||
var temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
};
|
||||
return pSort(arr);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,156 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc800d
|
||||
title: Sorting algorithms/Quicksort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to sort an array elements using the <a href="https://en.wikipedia.org/wiki/Quicksort"><i>quicksort</i></a> algorithm. The function should return the sorted array.
|
||||
The elements must have a <a href="https://en.wikipedia.org/wiki/Weak_ordering">strict weak order</a> and the index of the array can be of any discrete type.
|
||||
Quicksort, also known as <i>partition-exchange sort</i>, uses these steps.
|
||||
<ol>
|
||||
<li>Choose any element of the array to be the pivot.</li>
|
||||
<li>Divide all other elements (except the pivot) into two partitions.</li>
|
||||
<ul>
|
||||
<li>All elements less than the pivot must be in the first partition.</li>
|
||||
<li>All elements greater than the pivot must be in the second partition.</li>
|
||||
</ul>
|
||||
<li>Use recursion to sort both partitions.</li>
|
||||
<li>Join the first sorted partition, the pivot, and the second sorted partition.</li>
|
||||
</ol>
|
||||
The best pivot creates partitions of equal length (or lengths differing by <b>1</b>).
|
||||
The worst pivot creates an empty partition (for example, if the pivot is the first or last element of a sorted array).
|
||||
The run-time of Quicksort ranges from <i><a href="http://rosettacode.org/wiki/O">O</a>(n </i>log<i> n)</i> with the best pivots, to <i><a href="http://rosettacode.org/wiki/O">O</a>(n<sup>2</sup>)</i> with the worst pivots, where <i>n</i> is the number of elements in the array.
|
||||
This is a simple quicksort algorithm, adapted from Wikipedia.
|
||||
<pre>
|
||||
<b>function</b> <i>quicksort</i>(array)
|
||||
less, equal, greater <b>:=</b> three empty arrays
|
||||
<b>if</b> length(array) > 1
|
||||
pivot <b>:=</b> <i>select any element of</i> array
|
||||
<b>for each</b> x <b>in</b> array
|
||||
<b>if</b> x < pivot <b>then add</b> x <b>to</b> less
|
||||
<b>if</b> x = pivot <b>then add</b> x <b>to</b> equal
|
||||
<b>if</b> x > pivot <b>then add</b> x <b>to</b> greater
|
||||
quicksort(less)
|
||||
quicksort(greater)
|
||||
array <b>:=</b> concatenate(less, equal, greater)
|
||||
</pre>
|
||||
A better quicksort algorithm works in place, by swapping elements within the array, to avoid the memory allocation of more arrays.
|
||||
<pre>
|
||||
<b>function</b> <i>quicksort</i>(array)
|
||||
<b>if</b> length(array) > 1
|
||||
pivot <b>:=</b> <i>select any element of</i> array
|
||||
left <b>:= first index of</b> array
|
||||
right <b>:=</b> <b>last index of</b> array
|
||||
<b>while</b> left ≤ right
|
||||
<b>while</b> array[left] < pivot
|
||||
left := left + 1
|
||||
<b>while</b> array[right] > pivot
|
||||
right := right - 1
|
||||
<b>if</b> left ≤ right
|
||||
<b>swap</b> array[left] <b>with</b> array[right]
|
||||
left := left + 1
|
||||
right := right - 1
|
||||
quicksort(array <b>from first index to</b> right)
|
||||
quicksort(array <b>from</b> left <b>to last index</b>)
|
||||
</pre>
|
||||
Quicksort has a reputation as the fastest sort. Optimized variants of quicksort are common features of many languages and libraries. One often contrasts quicksort with <a href="http://rosettacode.org/wiki/../Merge sort">merge sort</a>, because both sorts have an average time of <i><a href="http://rosettacode.org/wiki/O">O</a>(n </i>log<i> n)</i>.
|
||||
<blockquote>
|
||||
<i>"On average, mergesort does fewer comparisons than quicksort, so it may be better when complicated comparison routines are used. Mergesort also takes advantage of pre-existing order, so it would be favored for using sort() to merge several sorted arrays. On the other hand, quicksort is often faster for small arrays, and on arrays of a few distinct values, repeated many times."</i> — <a href="http://perldoc.perl.org/sort.html">http://perldoc.perl.org/sort.html</a>
|
||||
</blockquote>
|
||||
Quicksort is at one end of the spectrum of divide-and-conquer algorithms, with merge sort at the opposite end.
|
||||
<ul>
|
||||
<li>Quicksort is a conquer-then-divide algorithm, which does most of the work during the partitioning and the recursive calls. The subsequent reassembly of the sorted partitions involves trivial effort.</li>
|
||||
<li>Merge sort is a divide-then-conquer algorithm. The partioning happens in a trivial way, by splitting the input array in half. Most of the work happens during the recursive calls and the merge phase.</li>
|
||||
</ul>
|
||||
With quicksort, every element in the first partition is less than or equal to every element in the second partition. Therefore, the merge phase of quicksort is so trivial that it needs no mention!
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>quickSort</code> should be a function.
|
||||
testString: assert(typeof quickSort == 'function', '<code>quickSort</code> should be a function.');
|
||||
- text: <code>quickSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(quickSort([25, 32, 12, 7, 20])), '<code>quickSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>quickSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(quickSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>quickSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>quickSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(quickSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>quickSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>quickSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(quickSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>quickSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>quickSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(quickSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>quickSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>quickSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(quickSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>quickSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function quickSort (array) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function quickSort (array) {
|
||||
|
||||
function swap(i, j) {
|
||||
var t = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = t;
|
||||
}
|
||||
|
||||
function quicksort(left, right) {
|
||||
|
||||
if (left < right) {
|
||||
var pivot = array[left + Math.floor((right - left) / 2)],
|
||||
left_new = left,
|
||||
right_new = right;
|
||||
|
||||
do {
|
||||
while (array[left_new] < pivot) {
|
||||
left_new += 1;
|
||||
}
|
||||
while (pivot < array[right_new]) {
|
||||
right_new -= 1;
|
||||
}
|
||||
if (left_new <= right_new) {
|
||||
swap(left_new, right_new);
|
||||
left_new += 1;
|
||||
right_new -= 1;
|
||||
}
|
||||
} while (left_new <= right_new);
|
||||
|
||||
quicksort(left, right_new);
|
||||
quicksort(left_new, right);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
quicksort(0, array.length - 1);
|
||||
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,84 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc800e
|
||||
title: Sorting algorithms/Radix sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to sort an integer array with the <a href="https://en.wikipedia.org/wiki/Radix sort">radix sort algorithm</a>. The function should return the sorted array.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>radixSort</code> should be a function.
|
||||
testString: assert(typeof radixSort == 'function', '<code>radixSort</code> should be a function.');
|
||||
- text: <code>radixSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(radixSort([25, 32, 12, 7, 20])), '<code>radixSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>radixSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(radixSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>radixSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>radixSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(radixSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>radixSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>radixSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(radixSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>radixSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>radixSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(radixSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>radixSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>radixSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(radixSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>radixSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function radixSort (old) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function radixSort (old) {
|
||||
for (var shift = 52; shift > -1; shift--) {
|
||||
var tmp = (function(s) {
|
||||
var a = [];
|
||||
while (s-- > 0)
|
||||
a.push(0);
|
||||
return a;
|
||||
})(old.length);
|
||||
var j = 0;
|
||||
for (var i = 0; i < old.length; i++) {
|
||||
var move = old[i] << shift >= 0;
|
||||
if (shift === 0 ? !move : move) {
|
||||
tmp[j] = old[i];
|
||||
j++;
|
||||
} else {
|
||||
old[i - j] = old[i];
|
||||
}
|
||||
};
|
||||
for (var i = j; i < tmp.length; i++) {
|
||||
tmp[i] = old[i - j];
|
||||
};
|
||||
old = tmp;
|
||||
};
|
||||
return old;
|
||||
};
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc800f
|
||||
title: Sorting algorithms/Selection sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to sort an <a href="http://rosettacode.org/wiki/array">array</a> (or list) of elements using the Selection sort algorithm. The function should return the sorted array.
|
||||
It works as follows:
|
||||
First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted.
|
||||
Its asymptotic complexity is <a href="http://rosettacode.org/wiki/O">O</a>(n<sup>2</sup>) making it inefficient on large arrays.
|
||||
Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM.
|
||||
No other sorting algorithm has less data movement.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>selectionSort</code> should be a function.
|
||||
testString: assert(typeof selectionSort == 'function', '<code>selectionSort</code> should be a function.');
|
||||
- text: <code>selectionSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(selectionSort([25, 32, 12, 7, 20])), '<code>selectionSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>selectionSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(selectionSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>selectionSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>selectionSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(selectionSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>selectionSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>selectionSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(selectionSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>selectionSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>selectionSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(selectionSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>selectionSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>selectionSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(selectionSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>selectionSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function selectionSort (nums) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function selectionSort (nums) {
|
||||
var len = nums.length;
|
||||
for(var i = 0; i < len; i++) {
|
||||
var minAt = i;
|
||||
for(var j = i + 1; j < len; j++) {
|
||||
if(nums[j] < nums[minAt])
|
||||
minAt = j;
|
||||
}
|
||||
|
||||
if(minAt != i) {
|
||||
var temp = nums[i];
|
||||
nums[i] = nums[minAt];
|
||||
nums[minAt] = temp;
|
||||
}
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8010
|
||||
title: Sorting algorithms/Shell sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to sort an array of elements using the <a href="https://en.wikipedia.org/wiki/Shell sort">Shell sort</a> algorithm, a diminishing increment sort. The function should return the sorted array.
|
||||
The Shell sort (also known as Shellsort or Shell's method) is named after its inventor, Donald Shell, who published the algorithm in 1959.
|
||||
Shell sort is a sequence of interleaved insertion sorts based on an increment sequence. The increment size is reduced after each pass until the increment size is 1.
|
||||
With an increment size of 1, the sort is a basic insertion sort, but by this time the data is guaranteed to be almost sorted, which is insertion sort's "best case".
|
||||
Any sequence will sort the data as long as it ends in 1, but some work better than others.
|
||||
Empirical studies have shown a geometric increment sequence with a ratio of about 2.2 work well in practice.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>shellSort</code> should be a function.
|
||||
testString: assert(typeof shellSort == 'function', '<code>shellSort</code> should be a function.');
|
||||
- text: <code>shellSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(shellSort([25, 32, 12, 7, 20])), '<code>shellSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>shellSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(shellSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>shellSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>shellSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(shellSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>shellSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>shellSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(shellSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>shellSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>shellSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(shellSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>shellSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>shellSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(shellSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>shellSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function shellSort (a) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function shellSort (a) {
|
||||
for (var h = a.length; h > 0; h = parseInt(h / 2)) {
|
||||
for (var i = h; i < a.length; i++) {
|
||||
var k = a[i];
|
||||
for (var j = i; j >= h && k < a[j - h]; j -= h)
|
||||
a[j] = a[j - h];
|
||||
a[j] = k;
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8012
|
||||
title: Sorting algorithms/Stooge sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to permform <a href="https://en.wikipedia.org/wiki/Stooge sort">Stooge Sort</a> on an array of integers. The function should return a sorted array.
|
||||
The Stooge Sort algorithm is as follows:
|
||||
<pre>
|
||||
<b>algorithm</b> stoogesort(<b>array</b> L, i = 0, j = <b>length</b>(L)-1)
|
||||
<b>if</b> L[j] < L[i] <b>then</b>
|
||||
L[i] <b>↔</b> L[j]
|
||||
<b>if</b> j - i > 1 <b>then</b>
|
||||
t <b>:=</b> (j - i + 1)/3
|
||||
stoogesort(L, i , j-t)
|
||||
stoogesort(L, i+t, j )
|
||||
stoogesort(L, i , j-t)
|
||||
<b>return</b> L
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>stoogeSort</code> should be a function.
|
||||
testString: assert(typeof stoogeSort == 'function', '<code>stoogeSort</code> should be a function.');
|
||||
- text: <code>stoogeSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(stoogeSort([25, 32, 12, 7, 20])), '<code>stoogeSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>stoogeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(stoogeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>stoogeSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>stoogeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(stoogeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>stoogeSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>stoogeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(stoogeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>stoogeSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>stoogeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(stoogeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>stoogeSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>stoogeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(stoogeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>stoogeSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function stoogeSort (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function stoogeSort (arr) {
|
||||
function stoogeSortRecurse(array, i, j) {
|
||||
if (j === undefined) {
|
||||
j = array.length - 1;
|
||||
}
|
||||
|
||||
if (i === undefined) {
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if (array[j] < array[i]) {
|
||||
var aux = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = aux;
|
||||
}
|
||||
|
||||
if (j - i > 1) {
|
||||
var t = Math.floor((j - i + 1) / 3);
|
||||
stoogeSortRecurse(array, i, j - t);
|
||||
stoogeSortRecurse(array, i + t, j);
|
||||
stoogeSortRecurse(array, i, j - t);
|
||||
}
|
||||
}
|
||||
stoogeSortRecurse(arr);
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc8013
|
||||
title: Sorting algorithms/Strand sort
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function to sort an array using the <a href="https://en.wikipedia.org/wiki/Strand sort">Strand sort</a>. The function should return the sorted array.
|
||||
This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>strandSort</code> should be a function.
|
||||
testString: assert(typeof strandSort == 'function', '<code>strandSort</code> should be a function.');
|
||||
- text: <code>strandSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(strandSort([25, 32, 12, 7, 20])), '<code>strandSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>strandSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(strandSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>strandSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>strandSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
testString: assert.deepEqual(strandSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], '<code>strandSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.');
|
||||
- text: <code>strandSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.
|
||||
testString: assert.deepEqual(strandSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], '<code>strandSort([43, 36, 20, 34, 24])</code> should return <code>[20, 24, 34, 36, 43]</code>.');
|
||||
- text: <code>strandSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.
|
||||
testString: assert.deepEqual(strandSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], '<code>strandSort([12, 33, 26, 18, 1, 16, 38])</code> should return <code>[1, 12, 16, 18, 26, 33, 38]</code>.');
|
||||
- text: <code>strandSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.
|
||||
testString: assert.deepEqual(strandSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], '<code>strandSort([3, 39, 48, 16, 1, 4, 29])</code> should return <code>[1, 3, 4, 16, 29, 39, 48]</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function strandSort (list) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function strandSort (list) {
|
||||
|
||||
function merge(left, right) {
|
||||
var result = [];
|
||||
while (left.length != 0 && right.length != 0) {
|
||||
if (left[0] <= right[0])
|
||||
result.push(left.shift());
|
||||
else
|
||||
result.push(right.shift());
|
||||
}
|
||||
result.push.apply(result, left);
|
||||
result.push.apply(result, right);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (list.length <= 1) return list;
|
||||
var result = [];
|
||||
while (list.length > 0) {
|
||||
var sorted = [];
|
||||
sorted.push(list.shift());
|
||||
var len = list.length;
|
||||
for (var i = 1; i < len; i++) {
|
||||
var elem = list[i];
|
||||
if (sorted[i - 1] <= elem) {
|
||||
sorted.push(elem);
|
||||
sorted.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
result = merge(result, sorted);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
Reference in New Issue
Block a user