Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7471d10

Browse files
better performance
1 parent 631267a commit 7471d10

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

‎book/chapters/insertion-sort.adoc‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
Insertion sort is one of the most natural way of sorting. If you are given some cards that's probably how you are going to sort the.
44

5-
.This is how it works:
6-
. Start with the first element, assume that everything to its left is sorted and everything to the right is not.
7-
. Move to the next element.
5+
Insertion sorts visit elements one by one. Everything to the left of the current element is curretly sorted and everything to the right is not.
86

9-
. Take the first element and consider it sorted.
10-
. Take the 2nd element, compare it with the first element. It 2nd is smaller swap it with the first element.
11-
. Go to the 3rd element compare it with the already sorted elements and if any of them is smaller, swap them.
7+
.This is how it works:
8+
. Visit 2nd. If the current element is smaller than the previous one move to the place it should be.
9+
. Visit next element and do the same thing. The left is always sorted.
1210
. Repeat for the rest of the array.
1311

1412
More efficient in practice than most other simple quadratic (i.e., O(n^2^)) algorithms such as <<Selection Sort>> or <<Bubble Sort>>.

‎src/algorithms/sorting/insertion-sort.js‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
const { swap } = require('./sorting-common');
2-
31
function insertionSort(collection) {
42
const array = Array.from(collection);
53

6-
for (let outer = 1; outer < array.length; outer += 1) {
4+
for (let outer = 0; outer < array.length; outer += 1) {
5+
const insert = array[outer];
76
let inner = outer - 1;
8-
while (inner >= 0 && array[outer] < array[inner]) {
7+
while (inner >= 0 && array[inner] > insert) {
8+
array[inner + 1] = array[inner];
99
inner -= 1;
1010
}
11-
if (outer !== inner + 1) {
12-
const [insert] = array.splice(outer, 1);
13-
array.splice(inner + 1, 0, insert);
14-
}
11+
array[inner + 1] = insert;
1512
}
1613
return array;
1714
}

‎src/algorithms/sorting/sorting-common.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// tag::swap[]
22
/**
33
* Swap array elements in place
4+
* Runtime: O(1)
45
* @param {array} array to be modified
56
* @param {integer} from index of the first element
67
* @param {integer} to index of the 2nd element
@@ -10,6 +11,20 @@ function swap(array, from, to) {
1011
}
1112
// end::swap[]
1213

14+
/**
15+
* Move an element in an array *from* a postion *to* another.
16+
* Runtime: O(n)
17+
* @param {array} array
18+
* @param {integer} from index of the element to remove (source)
19+
* @param {integer} to index where the removed element would be move (destination)
20+
*/
21+
function moveElement(array, from, to) {
22+
if (from === to + 1) return;
23+
const [elementToInsert] = array.splice(from, 1); // delete from position
24+
array.splice(to + 1, 0, elementToInsert); // insert element in to the position.
25+
}
26+
1327
module.exports = {
1428
swap,
29+
moveElement,
1530
};

‎src/algorithms/sorting/sorting.spec.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ const sortingAlgorithms = [
55

66
sortingAlgorithms.forEach((sort) => {
77
describe(`Sorting with ${sort.name}`, () => {
8+
it('should work with zero numbers', () => {
9+
expect(sort([])).toEqual([]);
10+
});
11+
12+
it('should work with one number', () => {
13+
expect(sort([3])).toEqual([3]);
14+
});
15+
816
it('should sort numbers', () => {
917
expect(sort([3, 5, 0])).toEqual([0, 3, 5]);
1018
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /