Created
February 26, 2022 22:14
-
-
Save see-why/2e22f920aa2759e0e49f880b02afd0c7 to your computer and use it in GitHub Desktop.
HackerRank Insertion Sort - Part 1 solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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