Added sorting algorithms
This commit is contained in:
parent
2053e99cd5
commit
c752d57611
@ -26,8 +26,8 @@ Where the correct result would be:
|
||||
tests:
|
||||
- text: <code>sortDisjoint</code> should be a function.
|
||||
testString: assert(typeof sortDisjoint == 'function', '<code>sortDisjoint</code> should be a function.');
|
||||
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return a array.
|
||||
testString: assert(Array.isArray(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])), '<code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return a array.');
|
||||
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return an array.
|
||||
testString: assert(Array.isArray(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])), '<code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return an array.');
|
||||
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return <code>[7, 0, 5, 4, 3, 2, 1, 6]</code>.
|
||||
testString: assert.deepEqual(sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]), [7, 0, 5, 4, 3, 2, 1, 6], '<code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])</code> should return <code>[7, 0, 5, 4, 3, 2, 1, 6]</code>.');
|
||||
- text: <code>sortDisjoint([7, 6, 5, 4, 3, 2, 1, 0], [1, 2, 5, 6])</code> should return <code>[7, 1, 2, 4, 3, 5, 6, 0]</code>.
|
||||
|
@ -13,6 +13,7 @@ Write a function to sort an array (or list) of strings in order of descending le
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
@ -20,8 +21,8 @@ Write a function to sort an array (or list) of strings in order of descending le
|
||||
tests:
|
||||
- text: <code>lengthSorter</code> should be a function.
|
||||
testString: assert(typeof lengthSorter == 'function', '<code>lengthSorter</code> should be a function.');
|
||||
- text: <code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return a array.
|
||||
testString: assert(Array.isArray(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])), '<code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return a array.');
|
||||
- text: <code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return an array.
|
||||
testString: assert(Array.isArray(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])), '<code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return an array.');
|
||||
- text: <code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return <code>["strings", "sample", "sorted", "Here", "some", "are", "be", "to"]</code>.
|
||||
testString: assert.deepEqual(lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"]), ["strings", "sample", "sorted", "Here", "some", "are", "be", "to"], '<code>lengthSorter(["Here", "are", "some", "sample", "strings", "to", "be", "sorted"])</code> should return <code>["strings", "sample", "sorted", "Here", "some", "are", "be", "to"]</code>.');
|
||||
- text: <code>lengthSorter(["I", "hope", "your", "day", "is", "going", "good", "?"])</code> should return <code>["going", "good", "hope", "your", "day", "is", "?","I"]</code>.
|
||||
@ -41,7 +42,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function lengthSorter (arr) {
|
||||
function lengthSorter(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
@ -6,9 +6,9 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Sort an array of positive integers using the <a href="https://en.wikipedia.org/wiki/Bead_sort">Bead Sort Algorithm</a>.
|
||||
Sort an array of positive integers using the <a href="https://en.wikipedia.org/wiki/Bead_sort" target="_blank">Bead Sort Algorithm</a>.
|
||||
A <i>bead sort</i> is also known as a <i>gravity sort</i>.
|
||||
Algorithm has O(S), where S is the sum of the integers in the input set: Each bead is moved individually.
|
||||
The algorithm has O(S), where S is the sum of the integers in the input set: Each bead is moved individually.
|
||||
This is the case when bead sort is implemented without a mechanism to assist in finding empty spaces below the beads, such as in software implementations.
|
||||
</section>
|
||||
|
||||
@ -24,8 +24,8 @@ This is the case when bead sort is implemented without a mechanism to assist in
|
||||
tests:
|
||||
- text: <code>beadSort</code> should be a function.
|
||||
testString: assert(typeof beadSort == 'function', '<code>beadSort</code> should be a function.');
|
||||
- text: <code>beadSort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(beadSort([25, 32, 12, 7, 20])), '<code>beadSort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>beadSort([25, 32, 12, 7, 20])</code> should return an array.
|
||||
testString: assert(Array.isArray(beadSort([25, 32, 12, 7, 20])), '<code>beadSort([25, 32, 12, 7, 20])</code> should return an array.');
|
||||
- text: <code>beadSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(beadSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>beadSort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>beadSort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
@ -45,7 +45,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function beadSort (arr) {
|
||||
function beadSort(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
@ -6,15 +6,17 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<a href="https://en.wikipedia.org/wiki/Bogosort">Bogosort</a> a list of numbers.
|
||||
<a href="https://en.wikipedia.org/wiki/Bogosort" target="_blank">Bogosort</a> a list of numbers.
|
||||
Bogosort simply shuffles a collection randomly until it is sorted.
|
||||
"Bogosort" is a perversely inefficient algorithm only used as an in-joke.
|
||||
Its average run-time is O(n!) because the chance that any given shuffle of a set will end up in sorted order is about one in <i>n</i> factorial, and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence.
|
||||
Its best case is O(n) since a single pass through the elements may suffice to order them.
|
||||
Pseudocode:
|
||||
<pre>
|
||||
<b>while not</b> InOrder(list) <b>do</b>
|
||||
Shuffle(list)
|
||||
Shuffle(list)
|
||||
<b>done</b>
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
@ -29,8 +31,8 @@ Shuffle(list)
|
||||
tests:
|
||||
- text: <code>bogosort</code> should be a function.
|
||||
testString: assert(typeof bogosort == 'function', '<code>bogosort</code> should be a function.');
|
||||
- text: <code>bogosort([25, 32, 12, 7, 20])</code> should return a array.
|
||||
testString: assert(Array.isArray(bogosort([25, 32, 12, 7, 20])), '<code>bogosort([25, 32, 12, 7, 20])</code> should return a array.');
|
||||
- text: <code>bogosort([25, 32, 12, 7, 20])</code> should return an array.
|
||||
testString: assert(Array.isArray(bogosort([25, 32, 12, 7, 20])), '<code>bogosort([25, 32, 12, 7, 20])</code> should return an array.');
|
||||
- text: <code>bogosort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.
|
||||
testString: assert.deepEqual(bogosort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], '<code>bogosort([25, 32, 12, 7, 20])</code> should return <code>[7, 12, 20, 25, 32]</code>.');
|
||||
- text: <code>bogosort([38, 45, 35, 8, 13])</code> should return <code>[8, 13, 35, 38, 45]</code>.
|
||||
@ -50,7 +52,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function bogosort (v) {
|
||||
function bogosort(v) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
@ -6,7 +6,7 @@ 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>
|
||||
The cocktail shaker sort is an improvement on the <a href="http://rosettacode.org/wiki/Bubble Sort" target="_blank">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" target="_blank">wikipedia</a>):</p>
|
||||
<pre>
|
||||
<b>function</b> <i>cocktailSort</i>( A : list of sortable items )
|
||||
<b>do</b>
|
||||
@ -29,12 +29,11 @@ The cocktail shaker sort is an improvement on the <a href="http://rosettacode.or
|
||||
<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'>
|
||||
|
||||
Write a function that sorts a given array using cocktail sort.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
@ -44,8 +43,8 @@ Write a function that sorts a given array using cocktail sort.
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(cocktailSort([25, 32, 12, 7, 20])), '<code>cocktailSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
@ -65,7 +64,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function cocktailSort (arr) {
|
||||
function cocktailSort(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
@ -77,7 +76,7 @@ function cocktailSort (arr) {
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function cocktailSort (arr) {
|
||||
function cocktailSort(arr) {
|
||||
let isSorted = true;
|
||||
while (isSorted) {
|
||||
for (let i = 0; i < arr.length - 1; i++) {
|
||||
|
@ -7,13 +7,13 @@ 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.
|
||||
The <b>Comb Sort</b> is a variant of the <a href="http://rosettacode.org/wiki/Bubble Sort" target="_blank">Bubble Sort</a>.
|
||||
Like the <a href="http://rosettacode.org/wiki/Shell sort" target="_blank">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>
|
||||
<li>the Wikipedia article: <a href="https://en.wikipedia.org/wiki/Comb sort" target="_blank">Comb sort</a>.</li>
|
||||
</ul>
|
||||
Variants:
|
||||
<ul>
|
||||
@ -45,12 +45,11 @@ Pseudocode:
|
||||
<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'>
|
||||
|
||||
Write a function that sorts a given array using Comb sort.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
@ -60,8 +59,8 @@ Write a function that sorts a given array using Comb sort.
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(combSort([25, 32, 12, 7, 20])), '<code>combSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
@ -81,7 +80,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function combSort (arr) {
|
||||
function combSort(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
@ -93,7 +92,7 @@ function combSort (arr) {
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function combSort (arr) {
|
||||
function combSort(arr) {
|
||||
function is_array_sorted(arr) {
|
||||
var sorted = true;
|
||||
for (var i = 0; i < arr.length - 1; i++) {
|
||||
|
@ -6,7 +6,7 @@ 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>.
|
||||
Gnome sort is a sorting algorithm which is similar to <a href="http://rosettacode.org/wiki/Insertion sort" target="_blank">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" target="_blank">Bubble Sort</a>.
|
||||
The pseudocode for the algorithm is:
|
||||
<pre>
|
||||
<b>function</b> <i>gnomeSort</i>(a[0..size-1])
|
||||
@ -27,12 +27,11 @@ The pseudocode for the algorithm is:
|
||||
<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'>
|
||||
|
||||
Write a function to implement the above pseudo code. The function should return the sorted array.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
@ -42,8 +41,8 @@ Write a function to implement the above pseudo code. The function should return
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(gnomeSort([25, 32, 12, 7, 20])), '<code>gnomeSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
|
@ -6,11 +6,11 @@ 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.
|
||||
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" target="_blank">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
|
||||
<b>6 7 8 9</b> 2 5 3 4 1<br>
|
||||
After:
|
||||
<b>9 8 7 6</b> 2 5 3 4 1
|
||||
</pre>
|
||||
@ -29,8 +29,8 @@ Only one end of the list can be flipped; this should be the low end, but the hig
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(pancakeSort([25, 32, 12, 7, 20])), '<code>pancakeSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
@ -50,7 +50,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function pancakeSort (arr) {
|
||||
function pancakeSort(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
@ -27,8 +27,8 @@ Pseudocode:
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(permutationSort([25, 32, 12, 7, 20])), '<code>permutationSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
@ -48,7 +48,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function permutationSort (arr) {
|
||||
function permutationSort(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
@ -60,7 +60,7 @@ function permutationSort (arr) {
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function permutationSort (arr) {
|
||||
function permutationSort(arr) {
|
||||
function pSort(a) {
|
||||
var list = [];
|
||||
permute(a, a.length, list);
|
||||
|
@ -6,7 +6,7 @@ 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.
|
||||
Write a function to sort an array of elements using the <a href="https://en.wikipedia.org/wiki/Shell sort" target="_blank">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".
|
||||
@ -26,8 +26,8 @@ Empirical studies have shown a geometric increment sequence with a ratio of abou
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(shellSort([25, 32, 12, 7, 20])), '<code>shellSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
@ -47,7 +47,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function shellSort (a) {
|
||||
function shellSort(a) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
@ -59,7 +59,7 @@ function shellSort (a) {
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function shellSort (a) {
|
||||
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];
|
||||
|
@ -6,7 +6,7 @@ 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.
|
||||
Write a function to permform <a href="https://en.wikipedia.org/wiki/Stooge sort" target="_blank">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)
|
||||
@ -33,8 +33,8 @@ The Stooge Sort algorithm is as follows:
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(stoogeSort([25, 32, 12, 7, 20])), '<code>stoogeSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
@ -54,7 +54,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function stoogeSort (arr) {
|
||||
function stoogeSort(arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
@ -66,7 +66,7 @@ function stoogeSort (arr) {
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function stoogeSort (arr) {
|
||||
function stoogeSort(arr) {
|
||||
function stoogeSortRecurse(array, i, j) {
|
||||
if (j === undefined) {
|
||||
j = array.length - 1;
|
||||
|
@ -6,7 +6,7 @@ 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.
|
||||
Write a function to sort an array using the <a href="https://en.wikipedia.org/wiki/Strand sort" target="_blank">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>
|
||||
|
||||
@ -22,8 +22,8 @@ This is a way of sorting numbers by extracting shorter sequences of already sort
|
||||
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 an array.
|
||||
testString: assert(Array.isArray(strandSort([25, 32, 12, 7, 20])), '<code>strandSort([25, 32, 12, 7, 20])</code> should return an 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>.
|
||||
@ -43,7 +43,7 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function strandSort (list) {
|
||||
function strandSort(list) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
@ -55,7 +55,7 @@ function strandSort (list) {
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function strandSort (list) {
|
||||
function strandSort(list) {
|
||||
|
||||
function merge(left, right) {
|
||||
var result = [];
|
||||
|
Loading…
x
Reference in New Issue
Block a user