Skip to content

Instantly share code, notes, and snippets.

@SparrowMike
Created November 6, 2022 02:45
Show Gist options
  • Save SparrowMike/2e4f27d878867bef74e11025a2758bff to your computer and use it in GitHub Desktop.
Save SparrowMike/2e4f27d878867bef74e11025a2758bff to your computer and use it in GitHub Desktop.
function insertionSort1(arr){
var currentVal;
for(var i = 1; i < arr.length; i++){
currentVal = arr[i];
for(var j = i - 1; j >= 0 && arr[j] > currentVal; j--) {
arr[j+1] = arr[j]
}
arr[j+1] = currentVal;
}
return arr;
}
// ===============================
let insertionSort2 = (inputArr) => {
for (let i = 1; i < inputArr.length; i++) {
let key = inputArr[i];
let j = i - 1;
while (j >= 0 && inputArr[j] > key) {
inputArr[j + 1] = inputArr[j];
j = j - 1;
}
inputArr[j + 1] = key;
}
return inputArr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment