Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sorting-javascript",
"version": "0.0.4",
"version": "0.0.5",
"description": "Sorting algorithms implemented in JS",
"repository": "neeleshroy/sorting-js",
"author": "Neelesh Roy",
Expand Down
14 changes: 14 additions & 0 deletions src/insertion-sort/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function insertionSort(arr) {
const unsorted = arr;

for (let i = 1; i < unsorted.length; i++) {
for (let j = 0; j < i; j++) {
if (unsorted[i] < unsorted[j]) {
const spliced = unsorted.splice(i, 1);
unsorted.splice(j, 0, spliced[0]);
}
}
}

return unsorted;
}
33 changes: 33 additions & 0 deletions test/insertion-sort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* sorting-js
*
* Copyright © 2018 Neelesh Roy. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { insertionSort } from '../src/insertion-sort';

describe('Insertion Sort', () => {

describe('Insertion - function', () => {

it('should return empty array', () => {
const test = [];

const out = insertionSort(test);

expect(out).to.eql([]);
});

it('should sort the elements in ascending order', () => {
const test = [2, 5, 4, 10, 5, 3, 2, 7];

const out = insertionSort(test, 'a');

expect(out).to.eql([2, 2, 3, 4, 5, 5, 7, 10]);
});
});
});