Skip to content

Instantly share code, notes, and snippets.

@yamadayuki
Last active December 8, 2016 17:49
Show Gist options
  • Save yamadayuki/c3ffa1d46d1275b50867eca32214a792 to your computer and use it in GitHub Desktop.
Save yamadayuki/c3ffa1d46d1275b50867eca32214a792 to your computer and use it in GitHub Desktop.
Insertion Sort
// 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