[알고리즘] 삽입정렬

성준영·2022년 7월 26일
0
const InsersionSort = (array) => {
  if (array.length <= 1) return array;

  for (let i = 1; i < array.length; i++) {
    let currentValue = array[i];
    let j;
    for (j = i - 1; j >= 0 && array[j] > currentValue; j--) {
      array[j + 1] = array[j];
    }
    array[j + 1] = currentValue;
  }
  return array;
};

console.log(InsersionSort([3, 1, 56, 7, 8, 10, -1]));
시간복잡도O(N)
BestO(N)
AverageO(N^2)
WorstO(N^2)
profile
기록해버리기

0개의 댓글