Skip to content

Commit 00f5936

Browse files
HereBeAndreAndrea Tota
andauthored
merge: Add alternative implementation for InsertionSort and relative test. Add additional algorithm description (TheAlgorithms#915)
Co-authored-by: Andrea Tota <tota@qi4m.com>
1 parent 98c46b4 commit 00f5936

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

Sorts/InsertionSort.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* In insertion sort, we divide the initial unsorted array into two parts;
2-
* sorted part and unsorted part. Initially the sorted part just has one
3-
* element (Array of only 1 element is a sorted array). We then pick up
4-
* element one by one from unsorted part; insert into the sorted part at
5-
* the correct position and expand sorted part one element at a time.
6-
*/
2+
* sorted part and unsorted part. Initially the sorted part just has one
3+
* element (Array of only 1 element is a sorted array). We then pick up
4+
* element one by one from unsorted part; insert into the sorted part at
5+
* the correct position and expand sorted part one element at a time.
6+
*/
7+
78
export function insertionSort (unsortedList) {
89
const len = unsortedList.length
910
for (let i = 1; i < len; i++) {
@@ -19,3 +20,43 @@ export function insertionSort (unsortedList) {
1920
unsortedList[j + 1] = tmp
2021
}
2122
}
23+
24+
/**
25+
* @function insertionSortAlternativeImplementation
26+
* @description InsertionSort is a stable sorting algorithm
27+
* @param {Integer[]} array - Array of integers
28+
* @return {Integer[]} - Sorted array
29+
* @see [InsertionSort](https://en.wikipedia.org/wiki/Quicksort)
30+
*/
31+
32+
/*
33+
* Big-O Analysis
34+
* Time Complexity
35+
- O(N^2) on average and worst case scenario
36+
- O(N) on best case scenario (when input array is already almost sorted)
37+
* Space Complexity
38+
- O(1)
39+
*/
40+
41+
export function insertionSortAlternativeImplementation (array) {
42+
const length = array.length
43+
if (length < 2) return array
44+
45+
for (let i = 1; i < length; i++) {
46+
// Take current element in array
47+
const currentItem = array[i]
48+
// Take index of previous element in array
49+
let j = i - 1
50+
51+
// While j >= 0 and previous element is greater than current element
52+
while (j >= 0 && array[j] > currentItem) {
53+
// Move previous, greater element towards the unsorted part
54+
array[j + 1] = array[j]
55+
j--
56+
}
57+
// Insert currentItem number at the correct position in sorted part.
58+
array[j + 1] = currentItem
59+
}
60+
// Return array sorted in ascending order
61+
return array
62+
}

Sorts/test/InsertionSort.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { insertionSortAlternativeImplementation } from '../InsertionSort'
2+
3+
describe('insertionSortAlternativeImplementation', () => {
4+
it('expects to work with empty array', () => {
5+
expect(insertionSortAlternativeImplementation([])).toEqual([])
6+
})
7+
8+
it('expects to return input array when array.length is less than 2', () => {
9+
const input = [3]
10+
expect(insertionSortAlternativeImplementation(input)).toEqual(input)
11+
})
12+
13+
it('expects to return array sorted in ascending order', () => {
14+
expect(insertionSortAlternativeImplementation([14, 11])).toEqual([11, 14])
15+
expect(insertionSortAlternativeImplementation([21, 22, 23])).toEqual([21, 22, 23])
16+
expect(insertionSortAlternativeImplementation([1, 3, 2, 3, 7, 2])).toEqual([1, 2, 2, 3, 3, 7])
17+
expect(insertionSortAlternativeImplementation([1, 6, 4, 5, 9, 2])).toEqual([1, 2, 4, 5, 6, 9])
18+
})
19+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy