-
Notifications
You must be signed in to change notification settings - Fork 267
/
InsertionSort.js
32 lines (26 loc) · 1.17 KB
/
InsertionSort.js
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
/*
Insertion Sort is a sorting algorithm that works as follows:
1. It iterates through the array from the second position onward.
2. Compares the current value with the previous one:
- If it's smaller, it swaps the two values and continues comparing with the previous one.
- If it's larger, it proceeds to analyze the next unsorted element in the array.
*/
function insertionSort(unsortedArray, start, end) {
for (var i = start + 1; i < end; i++) {
let currentPosition = i;
while (currentPosition > 0 && unsortedArray[currentPosition] < unsortedArray[currentPosition - 1]) {
swapPositions(unsortedArray, currentPosition, currentPosition - 1);
currentPosition--;
}
}
return unsortedArray;
}
function swapPositions(array, firstPosition, secondPosition) {
let firstValue = array[firstPosition];
let secondValue = array[secondPosition];
array[firstPosition] = secondValue;
array[secondPosition] = firstValue;
}
var unsortedArray = [54, 42, 11, 33, 24, 99, 77, 80];
let sortedArrayViaInsertionSort = insertionSort(unsortedArray, 0, unsortedArray.length);
console.log(sortedArrayViaInsertionSort);