fix(challenges): Remove duplicate Rosetta Code sorting problems (#35547)
<!-- Please follow this checklist and put an x in each of the boxes, like this: [x]. It will ensure that our team takes your pull request seriously. --> - [x] I have read [freeCodeCamp's contribution guidelines](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/CONTRIBUTING.md). - [x] My pull request has a descriptive title (not a vague title like `Update index.md`) - [x] My pull request targets the `master` branch of freeCodeCamp. - [x] None of my changes are plagiarized from another source without proper attribution. - [x] All the files I changed are in the same world language (for example: only English changes, or only Chinese changes, etc.) - [x] My changes do not use shortened URLs or affiliate links. <!--If your pull request closes a GitHub issue, replace the XXXXX below with the issue number.--> This PR is to remove duplicate Rosetta Code sorting problems that exist in other parts of the curriculum. Here are the problems that are being removed: * Sorting algorithms/Bubble sort * Sorting algorithms/Counting sort * Sorting algorithms/Heapsort * Sorting algorithms/Insertion sort * Sorting algorithms/Merge sort * Sorting algorithms/Quicksort * Sorting algorithms/Radix sort * Sorting algorithms/Selection sort This PR only removes the challenges in English. I could remove the other languages in this PR, or in another PR. Closes #34776
This commit is contained in:
committed by
mrugesh mohapatra
parent
08cb91afc5
commit
3e51ced41b
@ -1,87 +0,0 @@
|
||||
---
|
||||
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>
|
@ -1,96 +0,0 @@
|
||||
---
|
||||
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>
|
@ -1,146 +0,0 @@
|
||||
---
|
||||
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>
|
@ -1,85 +0,0 @@
|
||||
---
|
||||
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>
|
@ -1,132 +0,0 @@
|
||||
---
|
||||
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>
|
@ -1,156 +0,0 @@
|
||||
---
|
||||
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>
|
@ -1,84 +0,0 @@
|
||||
---
|
||||
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>
|
@ -1,81 +0,0 @@
|
||||
---
|
||||
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>
|
Reference in New Issue
Block a user