Skip to content

Commit b0247a5

Browse files
committedApr 13, 2018
Add insertion sort.
1 parent 37bbc53 commit b0247a5

File tree

8 files changed

+125
-8
lines changed

8 files changed

+125
-8
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* Sorting
3434
* [Bubble Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/bubble-sort)
3535
* [Selection Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/selection-sort)
36+
* [Insertion Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/insertion-sort)
3637

3738
## Running Tests
3839

@@ -98,4 +99,5 @@ Source: [Big O Cheat Sheet](http://bigocheatsheet.com/).
9899
| Name | Best | Average | Worst | Memory | Stable | Method | Notes |
99100
| --------------------- | :-----: | :-------: | :-----: | :-------: | :-------: | :------------ | :-------------- |
100101
| **Bubble sort** | n | n^2 | n^2 | 1 | Yes | Exchanging | Tiny code size |
102+
| **Insertion sort** | n | n^2 | n^2 | 1 | Yes | Insertion | O(n + d), in the worst case over sequences that have d inversions |
101103
| **Selection sort** | n^2 | n^2 | n^2 | 1 | No | Selection | Stable with O(n) extra space, for example using lists |

‎src/algorithms/sorting/bubble-sort/BubbleSort.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default class BubbleSort extends Sort {
1010
for (let i = 0; i < array.length; i += 1) {
1111
swapped = false;
1212

13+
// Call visiting callback.
14+
this.callbacks.visitingCallback(array[i]);
15+
1316
for (let j = 0; j < array.length - 1; j += 1) {
1417
// Call visiting callback.
1518
this.callbacks.visitingCallback(array[j]);

‎src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('BubbleSort', () => {
2121
});
2222

2323
it('should visit EQUAL array element specified number of times', () => {
24-
const expectedNumberOfVisits = 19;
24+
const expectedNumberOfVisits = 20;
2525

2626
SortTester.testAlgorithmTimeComplexity(
2727
BubbleSort,
@@ -31,7 +31,7 @@ describe('BubbleSort', () => {
3131
});
3232

3333
it('should visit SORTED array element specified number of times', () => {
34-
const expectedNumberOfVisits = 19;
34+
const expectedNumberOfVisits = 20;
3535

3636
SortTester.testAlgorithmTimeComplexity(
3737
BubbleSort,
@@ -41,7 +41,7 @@ describe('BubbleSort', () => {
4141
});
4242

4343
it('should visit NOT SORTED array element specified number of times', () => {
44-
const expectedNumberOfVisits = 266;
44+
const expectedNumberOfVisits = 280;
4545

4646
SortTester.testAlgorithmTimeComplexity(
4747
BubbleSort,
@@ -51,7 +51,7 @@ describe('BubbleSort', () => {
5151
});
5252

5353
it('should visit REVERSE SORTED array element specified number of times', () => {
54-
const expectedNumberOfVisits = 380;
54+
const expectedNumberOfVisits = 400;
5555

5656
SortTester.testAlgorithmTimeComplexity(
5757
BubbleSort,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Sort from '../Sort';
2+
3+
export default class InsertionSort extends Sort {
4+
sort(originalArray) {
5+
const array = originalArray.slice(0);
6+
7+
// Go through all array elements...
8+
for (let i = 0; i < array.length; i += 1) {
9+
let currentIndex = i;
10+
11+
// Call visiting callback.
12+
this.callbacks.visitingCallback(array[i]);
13+
14+
// Go and check if previous elements and greater then current one.
15+
// If this is the case then swap that elements.
16+
while (
17+
array[currentIndex - 1] &&
18+
this.comparator.lessThen(array[currentIndex], array[currentIndex - 1])
19+
) {
20+
// Call visiting callback.
21+
this.callbacks.visitingCallback(array[currentIndex - 1]);
22+
23+
// Swap the elements.
24+
const tmp = array[currentIndex - 1];
25+
array[currentIndex - 1] = array[currentIndex];
26+
array[currentIndex] = tmp;
27+
28+
// Shift current index left.
29+
currentIndex -= 1;
30+
}
31+
}
32+
33+
return array;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Insertion Sort
2+
3+
Insertion sort is a simple sorting algorithm that builds
4+
the final sorted array (or list) one item at a time.
5+
It is much less efficient on large lists than more
6+
advanced algorithms such as quicksort, heapsort, or merge
7+
sort.
8+
9+
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/4/42/Insertion_sort.gif)
10+
11+
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif)
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import InsertionSort from '../InsertionSort';
2+
import {
3+
equalArr,
4+
notSortedArr,
5+
reverseArr,
6+
sortedArr,
7+
SortTester,
8+
} from '../../SortTester';
9+
10+
describe('InsertionSort', () => {
11+
it('should sort array', () => {
12+
SortTester.testSort(InsertionSort);
13+
});
14+
15+
it('should sort array with custom comparator', () => {
16+
SortTester.testSortWithCustomComparator(InsertionSort);
17+
});
18+
19+
it('should do stable sorting', () => {
20+
SortTester.testSortStability(InsertionSort);
21+
});
22+
23+
it('should visit EQUAL array element specified number of times', () => {
24+
const expectedNumberOfVisits = 20;
25+
26+
SortTester.testAlgorithmTimeComplexity(
27+
InsertionSort,
28+
equalArr,
29+
expectedNumberOfVisits,
30+
);
31+
});
32+
33+
it('should visit SORTED array element specified number of times', () => {
34+
const expectedNumberOfVisits = 20;
35+
36+
SortTester.testAlgorithmTimeComplexity(
37+
InsertionSort,
38+
sortedArr,
39+
expectedNumberOfVisits,
40+
);
41+
});
42+
43+
it('should visit NOT SORTED array element specified number of times', () => {
44+
const expectedNumberOfVisits = 101;
45+
46+
SortTester.testAlgorithmTimeComplexity(
47+
InsertionSort,
48+
notSortedArr,
49+
expectedNumberOfVisits,
50+
);
51+
});
52+
53+
it('should visit REVERSE SORTED array element specified number of times', () => {
54+
const expectedNumberOfVisits = 210;
55+
56+
SortTester.testAlgorithmTimeComplexity(
57+
InsertionSort,
58+
reverseArr,
59+
expectedNumberOfVisits,
60+
);
61+
});
62+
});

‎src/algorithms/sorting/selection-sort/SelectionSort.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export default class SelectionSort extends Sort {
88
for (let i = 0; i < array.length - 1; i += 1) {
99
let minIndex = i;
1010

11+
// Call visiting callback.
12+
this.callbacks.visitingCallback(array[i]);
13+
1114
// Find minimum element in the rest of array.
1215
for (let j = i + 1; j < array.length; j += 1) {
1316
// Call visiting callback.

‎src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('SelectionSort', () => {
1717
});
1818

1919
it('should visit EQUAL array element specified number of times', () => {
20-
const expectedNumberOfVisits = 190;
20+
const expectedNumberOfVisits = 209;
2121

2222
SortTester.testAlgorithmTimeComplexity(
2323
SelectionSort,
@@ -27,7 +27,7 @@ describe('SelectionSort', () => {
2727
});
2828

2929
it('should visit SORTED array element specified number of times', () => {
30-
const expectedNumberOfVisits = 190;
30+
const expectedNumberOfVisits = 209;
3131

3232
SortTester.testAlgorithmTimeComplexity(
3333
SelectionSort,
@@ -37,7 +37,7 @@ describe('SelectionSort', () => {
3737
});
3838

3939
it('should visit NOT SORTED array element specified number of times', () => {
40-
const expectedNumberOfVisits = 190;
40+
const expectedNumberOfVisits = 209;
4141

4242
SortTester.testAlgorithmTimeComplexity(
4343
SelectionSort,
@@ -47,7 +47,7 @@ describe('SelectionSort', () => {
4747
});
4848

4949
it('should visit REVERSE SORTED array element specified number of times', () => {
50-
const expectedNumberOfVisits = 190;
50+
const expectedNumberOfVisits = 209;
5151

5252
SortTester.testAlgorithmTimeComplexity(
5353
SelectionSort,

0 commit comments

Comments
 (0)
Please sign in to comment.