Skip to content

Instantly share code, notes, and snippets.

@see-why
Created February 26, 2022 22:14
Show Gist options
  • Save see-why/2e22f920aa2759e0e49f880b02afd0c7 to your computer and use it in GitHub Desktop.
Save see-why/2e22f920aa2759e0e49f880b02afd0c7 to your computer and use it in GitHub Desktop.
HackerRank Insertion Sort - Part 1 solution
//https://www.hackerrank.com/challenges/insertionsort1/problem?isFullScreen=true
function insertionSort1(n, arr) {
// Write your code here
let max = arr[n-1]
for(let i=n-2; i>=0; i--){
if(arr[i] > max){
arr[i+1] = arr[i]
if (i == 0 && arr[i] > max){
console.log(...arr)
arr[0] = max
}
} else if (arr[i] < max) {
arr[i+1] = max
console.log(...arr)
break
}
console.log(...arr)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment