Last active
December 8, 2016 17:49
-
-
Save yamadayuki/c3ffa1d46d1275b50867eca32214a792 to your computer and use it in GitHub Desktop.
Insertion Sort
This file contains hidden or 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
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_A | |
// Test case | |
// 6 | |
// 5 2 4 6 1 3 | |
var input = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n'); | |
var n = +input.shift(); | |
var nums = input.shift().split(' ').map(Number); | |
for (var i = 0; i < nums.length; i++) { | |
var v = nums[i]; | |
var j = i - 1; | |
while (j >= 0 && nums[j] > v) { | |
nums[j + 1] = nums[j]; | |
j--; | |
} | |
nums[j + 1] = v; | |
console.log(nums.join(' ')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment