-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.js
More file actions
87 lines (63 loc) · 2.55 KB
/
InsertionSort.js
File metadata and controls
87 lines (63 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//insetion sort
// http://algorithms.dojo.news/static/Algorithms/index.html#LinkTarget_2140
// Array: Insertion Sort
// visualization with playing cards (scroll down):
// https://www.khanacademy.org/computing/computer-science/algorithms/insertion-sort/a/insertion-sort
// array / bar visualization:
// https://www.hackerearth.com/practice/algorithms/sorting/insertion-sort/visualize/
// insertionSort(arr){}
// - efficient for small data sets
// - constant memory / space O(1)
// Time Complexity
// - Best: n when array is already sorted
// - Average: O(n^2)
// - Worst: O(n^2) when the array is reverse sorted
// this sort splits the array into two portions (conceptually, not literally)
//
// the left portion will become sorted
// the right portion (that hasn't been iterated over yet) is unsorted
// can shift or swap target element until it reaches desired position
// shifting steps:
// 1. consider the first item as sorted
// 2. move to the next item
// 3. store current item in a temp var (to make this position availale to shift items)
// 4. if item to the left of current is greater than current item,
// shift the left item to the right
// 5. repeat step 4 as many times as needed
// 6. insert current item
// 7. move to the next item and repeat
// // swap steps:
// 1. consider the first item as sorted
// 2. move to the next item
// 4. if item to left of current item is less than current, swap
// 5. repeat step 4 until item to left is less than current item
// 6. move to next item and repeat
function insertionSort(arr){
for(var i=1; i<arr.length; i++){
// save the current element
var temp = arr[i];
// save the previous index
var j = i - 1;
while(j >= 0 && arr[j] > temp){
// shift to the right because it's greater than the item we're holding
arr[j + 1] = arr[j];
j--;
}
// while loop completes, move the temp into the proper location
arr[j + 1] = temp;
}
return arr;
}
function insertionSortDestructuredSwap(arr){
for (let i = 1; i < arr.length; i++){
let targetIdx = i;
let comparisonIdx = i - 1;
while(comparisonIdx >= 0 && arr[comparisonIdx] > arr[targetIdx]){
[arr[comparisonIdx], arr[targetIdx]] = [arr[targetIdx], arr[comparisonIdx]];
targetIdx = comparisonIdx;
comparisonIdx --;
}
return arr;
}
}
//<----------------------------------------------------------------------------------------------------------------------------<<<<<<