Skip to content

Commit 3728074

Browse files
committed
selection sort
1 parent 2905a60 commit 3728074

22 files changed

+58
-3
lines changed

book/chapters/selection-sort.adoc

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,58 @@ The selection sort as it names indicates choose the lowest element from the list
55
TIP: selection sort is a in-place sorting algorithms, it should be used when auxiliary memory is limited.
66

77
.Selection sort algorithm
8-
. Go throught the array a find the smallest element, swap it with index 0
9-
. Go to the next position and find the smallest from there on, swap the 2nd smallest element with the index 1
10-
. Repeat until the end of the array.
8+
. Start with the element in position 0.
9+
. Find minimum element in the rest of array. If a new minimun is found swap them.
10+
. Repeat with the element in postion 1 and so until the last one.
1111

12+
image:https://camo.githubusercontent.com/adfa2cdcc3825092dc405aadd87453571d6e0dc4/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f392f39342f53656c656374696f6e2d536f72742d416e696d6174696f6e2e676966[]
13+
14+
== Selection sort implementation
15+
For implementing the selection sort we need 2 indexes.
16+
17+
.Selection sort
18+
[source, javascript]
19+
----
20+
include::{codedir}/algorithms/selection-sort.js[tag=sort, indent=0]
21+
----
22+
23+
One index is for the position in question (selection/outer) and another one for finding the minimun in the rest of the array (element/inner).
24+
25+
The swap function is implemented as follows.
26+
27+
.Swap function
28+
[source, javascript]
29+
----
30+
include::{codedir}/algorithms/selection-sort.js[tag=swap, indent=0]
31+
----
32+
33+
It uses JavaScript ES6 destructing arrays.
34+
35+
.JavaScript Array destructuring
36+
****
37+
*Assignment separate from declaration*
38+
39+
A variable can be assign to its values using the destructing syntax.
40+
41+
[source, js]
42+
----
43+
let a, b;
44+
45+
[a, b] = [1, 2];
46+
console.log(a); //↪️ 1
47+
console.log(b); //️↪️ 2
48+
----
49+
50+
*Swapping variables*
51+
52+
Two variables values can be swapped in one destructuring expression.
53+
54+
[source, js]
55+
----
56+
[a, b] = [b, a];
57+
console.log(a); //↪️ 2
58+
console.log(b); //️↪️ 1
59+
----
60+
61+
Without destructuring assignment, swapping two values requires a temporary variable.
62+
****

src/algorithms/selection-sort.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// tag::swap[]
12
/**
23
* Swap array elements in place
34
* @param {array} array to be modified
@@ -7,7 +8,9 @@
78
function swap(array, from, to) {
89
[array[from], array[to]] = [array[to], array[from]];
910
}
11+
// end::swap[]
1012

13+
// tag::sort[]
1114
/**
1215
* Selection sort
1316
* @param {Array|Set} collection list to be sorted
@@ -30,5 +33,6 @@ function selectionSort(collection) {
3033

3134
return array;
3235
}
36+
// end::sort[]
3337

3438
module.exports = selectionSort;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)