๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์˜ˆ์‹œ์˜ ๋ชจ๋“  input์€ [10, 7, 9, 5, 1]๋กœ ํ†ต์ผ. ๋ฒ„๋ธ” / ์„ ํƒ / ์‚ฝ์ž… ์ •๋ ฌ ๋ชจ๋‘ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n²) ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š์•„ ๊ฑฐ์˜ ์“ฐ์ง„ ์•Š์ง€๋งŒ, ์ ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•  ๋• ์œ ์šฉ.

 

๋ฒ„๋ธ” ์ •๋ ฌ | Bubble Sort


[10, 7, 9, 5, 1] ๋ฐฐ์—ด์„ ๋ฒ„๋ธ” ์ •๋ ฌํ•˜๋Š” ๊ณผ์ •

๊ฑฐํ’ˆ์ด ์ผ์–ด๋‚œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋“ค์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ •๋ ฌ๋ผ์„œ ๋ฒ„๋ธ” ์ •๋ ฌ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๋‘ ์š”์†Œ๋ฅผ ๋ฌถ์–ด์„œ ๋น„๊ตํ•œ ํ›„ ํฐ ์ˆซ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด๋‚ธ๋‹ค. i๋ฒˆ์งธ ์ •๋ ฌ์„ ๋งˆ์น ๋•Œ๋งˆ๋‹ค “๋’ค์—์„œ i๋ฒˆ์งธ” ์ž๋ฆฌ์˜ ์ˆœ์„œ๊ฐ€ ํ™•์ •๋œ๋‹ค.

 

์‹œ๊ฐ„ ๋ณต์žก๋„ (์‚ฝ์ž… ์ •๋ ฌ๊ณผ ๋™์ผ)

  • Worst Case(์ •๋ ฌ์ด ์ „ํ˜€ ์•ˆ๋์„ ๋•Œ) : O(n²) — ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ
  • Best Case(์ด๋ฏธ ์ •๋ ฌ๋์„ ๋•Œ) : O(n)

 

์žฅ๋‹จ์  (์‚ฝ์ž… ์ •๋ ฌ๊ณผ ๋™์ผ)

Stable ์ •๋ ฌ : ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋Š” ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ์ง€ ์•Š๋Š” ๋ฐฉ์‹(Unstable ์ •๋ ฌ์€ ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋„ ์ˆœ์„œ ๋ฐ”๊ฟˆ)
In-place ์ •๋ ฌ : ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ ์—†์ด ๊ธฐ์กด ๋ฐฐ์—ด๋งŒ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ์ถฉ๋ถ„ํžˆ ๋ฌด์‹œํ• ๋งŒํ•œ ์ €์žฅ ๊ณต๊ฐ„๋งŒ ๋” ์‚ฌ์šฉํ•˜๋Š” ์ •๋ ฌ ๋ฐฉ์‹(Not In-place ์ •๋ ฌ์€ ์š”์†Œ ๊ฐœ์ˆ˜์— ๋น„๋ก€ํ•˜๋Š” ์ €์žฅ๊ณต๊ฐ„์„ ๋” ์‚ฌ์šฉํ•จ)

 

  • ์žฅ์  : ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฝ๊ณ , in place ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ๋จ. stable ์ •๋ ฌ ๋ฐฉ์‹
  • ๋‹จ์  : ์ž๋ฃŒ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์„ฑ๋Šฅ์ด ํ˜„์ €ํ•˜๊ฒŒ ๋–จ์–ด์ง. ex) ์ž๋ฃŒ๊ฐ€ 100๊ฐœ๋ผ๋ฉด 10000๋ฒˆ ์ˆœํšŒ

 

๊ตฌํ˜„

i๋ฅผ ํ•œ ๋ฒˆ ์ˆœํšŒํ•  ๋•Œ๋งˆ๋‹ค(์•ˆ์ชฝ for๋ฌธ ๋ชจ๋‘ ์ˆœํšŒ) ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ํฐ ์š”์†Œ(์ˆซ์ž)๊ฐ€ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋กœ ๋ฐ€๋ ค๋‚˜๋ฏ€๋กœ ๋‘ ๋ฒˆ์งธ for๋ฌธ j ์กฐ๊ฑด์‹์—์„œ i๋ฅผ ๋นผ๋ฉด ๋ถˆํ•„์š”ํ•œ ์ˆœํšŒ๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

const bubbleSort = (arr) => {
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; // swap
      }
    }
  }
  return arr;
};

bubbleSort([10, 7, 9, 5, 1]); // [1, 5, 7, 9, 10]
i = 0, j ์ตœ๋Œ€ 3(arr.length - 1 - i)
========================
j = 0 : [7, 10, 9, 5, 1]
j = 1 : [7, 9, 10, 5, 1]
j = 2 : [7, 9, 5, 10, 1]
j = 3 : [7, 9, 5, 1, 10]

i = 1, j ์ตœ๋Œ€ 2
========================
j = 0 : [7, 9, 5, 1, 10]
j = 1 : [7, 5, 9, 1, 10]
i = 2 : [7, 5, 1, 9, 10]

i = 2, j ์ตœ๋Œ€ 1
========================
j = 0 : [5, 7, 1, 9, 10]
j = 1 : [5, 1, 7, 9, 10]

i = 3, j ์ตœ๋Œ€ 0
========================
j = 0 : [1, 5, 7, 9, 10]

 

๋ฐฐ์—ด ๋‘ ์š”์†Œ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋Š” swap ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๋™์ผํ•˜๋‹ค.

if (arr[j] > arr[j + 1]) {
  // [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
  let temp = arr[j];
  arr[j] = arr[j + 1];
  arr[j + 1] = temp;
}

 

์„ ํƒ ์ •๋ ฌ | Selection Sort


[10, 7, 9, 5, 1] ๋ฐฐ์—ด์„ ์„ ํƒ ์ •๋ ฌํ•˜๋Š” ๊ณผ์ •

i ์ธ๋ฑ์Šค ์œ„์น˜์— ์˜ฌ ์š”์†Œ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ("์„ ํƒ"ํ•œ ์œ„์น˜์— ์˜ฌ ์š”์†Œ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜)

 

์„ ํƒ ์ •๋ ฌ์€ i ์ธ๋ฑ์Šค ์š”์†Œ๋ณด๋‹ค ์ž‘์€ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์€ ํ›„ i ์ธ๋ฑ์Šค ์š”์†Œ์™€ ๊ตํ™˜ํ•˜๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๋ฒ„๋ธ” ์ •๋ ฌ์€ i๋ฒˆ์งธ ์ˆœํšŒ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค “๋’ค์—์„œ” i๋ฒˆ์งธ ์ž๋ฆฌ์˜ ์ˆœ์„œ๊ฐ€ ์ •ํ•ด์ง€๋Š” ๋ฐ˜๋ฉด, ์„ ํƒ ์ •๋ ฌ์€ i๋ฒˆ์งธ ์ˆœํšŒ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค “์•ž์—์„œ” i ๋ฒˆ์งธ ์ž๋ฆฌ์˜ ์ˆœ์„œ๊ฐ€ ์ •ํ•ด์ง„๋‹ค.

 

์‹œ๊ฐ„ ๋ณต์žก๋„

  • Worst Case(์ •๋ ฌ์ด ์ „ํ˜€ ์•ˆ๋์„ ๋•Œ) : O(n²)
  • Best Case(์ด๋ฏธ ์ •๋ ฌ๋์„ ๋•Œ) : O(n²)

 

์žฅ๋‹จ์ 

  • ์žฅ์  : ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฝ๊ณ  in place ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ
  • ๋‹จ์  : ์„ฑ๋Šฅ์ด ๋งค์šฐ ๋–จ์–ด์ง, ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋„ ์œ„์น˜๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š” unstable ์ •๋ ฌ ๋ฐฉ์‹

 

๊ตฌํ˜„

const selectionSort = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    let min = i;
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] < arr[min]) {
        min = j;
      }
    }
    if (min !== i) {
      [arr[i], arr[min]] = [arr[min], arr[i]]; // swap
    }
  }
  return arr;
};

selectionSort([10, 7, 9, 5, 1]); // [1, 5, 7, 9, 10]
i = 0 / min = 0
===============
j = 1 : min = 1
j = 2 : min = 1
j = 3 : min = 3
j = 4 : min = 4
---------------
i !== min -> [1, 7, 9, 5, 10]

i = 1 / min = 1
===============
j = 2 : min = 1
j = 3 : min = 3
j = 4 : min = 3
---------------
i !== min -> [1, 5, 9, 7, 10]

i = 2 / min = 2
===============
j = 3 : min = 3
j = 4 : min = 3
---------------
i !== min -> [1, 5, 7, 9, 10]

i = 3 / min = 3
===============
j = 4 : min = 3
---------------
i === min -> [1, 5, 7, 9, 10]

 

์‚ฝ์ž… ์ •๋ ฌ | Insertion Sort


[10, 7, 9, 5, 1] ๋ฐฐ์—ด์„ ์‚ฝ์ž… ์ •๋ ฌํ•˜๋Š” ๊ณผ์ •

i ์ธ๋ฑ์Šค์˜ ์š”์†Œ๊ฐ€ ์‚ฝ์ž…๋  ์œ„์น˜๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

์‚ฝ์ž… ์ •๋ ฌ์€ 2๋ฒˆ์งธ(1๋ฒˆ ์ธ๋ฑ์Šค) ์š”์†Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์™ผ์ชฝ์— ์žˆ๋Š” ์š”์†Œ์™€ ๋น„๊ตํ•œ ํ›„, ์™ผ์ชฝ ์š”์†Œ๊ฐ€ ๋” ํฌ๋‹ค๋ฉด ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๋Š” ์ž‘์—…(Swap)์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋ฒ„๋ธ” ์ •๋ ฌ, ์„ ํƒ ์ •๋ ฌ๋ณด๋‹ค ํšจ์œจ์ ์ด๋‹ค.

1๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋น„๊ต ์‹œ์ž‘ํ•ด์„œ ์™ผ์ชฝ ๊ฐ’์ด ๋” ํฌ๋ฉด Swap / ์ž‘์œผ๋ฉด i++
---------------------------------------------------
i = 1 : 0 ๋ฒˆ ์ธ๋ฑ์Šค์™€ ๋น„๊ต 
i = 2 : 1 → 0 ๋ฒˆ ์ธ๋ฑ์Šค์™€ ๋น„๊ต 
i = 3 : 2 → 1 → 0 ๋ฒˆ ์ธ๋ฑ์Šค์™€ ๋น„๊ต 
...

 

์‹œ๊ฐ„ ๋ณต์žก๋„ (๋ฒ„๋ธ” ์ •๋ ฌ๊ณผ ๋™์ผ)

  • Worst Case(์ •๋ ฌ์ด ์ „ํ˜€ ์•ˆ๋์„ ๋•Œ) : O(n²) — ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ
  • Best Case(์ด๋ฏธ ์ •๋ ฌ๋์„ ๋•Œ) : O(n)

 

์žฅ๋‹จ์  (๋ฒ„๋ธ” ์ •๋ ฌ๊ณผ ๋™์ผ)

  • ์žฅ์  : ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฝ๊ณ , in place ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ๋จ. stable ์ •๋ ฌ ๋ฐฉ์‹
  • ๋‹จ์  : ์ž๋ฃŒ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์„ฑ๋Šฅ์ด ํ˜„์ €ํ•˜๊ฒŒ ๋–จ์–ด์ง. ex) ์ž๋ฃŒ๊ฐ€ 100๊ฐœ๋ผ๋ฉด 10000๋ฒˆ ์ˆœํšŒ

 

๊ตฌํ˜„

const insertionSort = (arr) => {
  for (let i = 1; i < arr.length; i++) {
    let j = i;
    while (j > 0 && arr[j - 1] > arr[j]) {
      // arr[j] ์™ผ์ชฝ ์š”์†Œ๊ฐ€ ๋” ํฐ์ง€ ํ™•์ธ
      [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]]; // swap
      j--;
    }
  }
  return arr;
};

insertionSort([10, 7, 9, 5, 1]); // [1, 5, 7, 9, 10]
i = 1
========================
j = 1 : [7, 10, 9, 5, 1]

i = 2
========================
j = 2 : [7, 9, 10, 5, 1]

i = 3
========================
j = 3 : [7, 9, 5, 10, 1]
j = 2 : [7, 5, 9, 10, 1]
j = 1 : [5, 7, 9, 10, 1] 

i = 4
========================
j = 4 : [5, 7, 9, 1, 10]
j = 3 : [5, 7, 1, 9, 10]
j = 2 : [5, 1, 7, 9, 10]
j = 1 : [1, 5, 7, 9, 10]

 

๋ ˆํผ๋Ÿฐ์Šค


 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•