과정1
과정2
과정3 (비교 값 빼두기)
과정4 (왼쪽 값과 비교)
과정5 (크면 더이상 비교x, 삽입)
과정6 (비교 값 빼두기)
과정7 (왼쪽 값과 비교)
과정8
과정9 (더이상 비교할 값이 없으면 비교x, 삽입)
과정 10 ~ (반복)
function insertSort(arr) {
const size = arr.length;
for(let i = 1; i < size; i++) {
let current = arr[i];
let tobeInsertedIdx = i - 1;
while(tobeInsertedIdx >= 0 && arr[tobeInsertedIdx] > current) {
arr[tobeInsertedIdx + 1] = arr[tobeInsertedIdx];
tobeInsertedIdx--;
}
arr[tobeInsertedIdx + 1] = current;
}
}
O($n$)
많은 이동
을 해야하므로 성능이 좋지 않다.O($n^2$)
O($n^2$)
O($n$)
(정렬이 되어있는 경우)