์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ/์•Œ๊ณ ๋ฆฌ์ฆ˜

JavaScript๋กœ ์•Œ์•„๋ณด๋Š” ๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)๊ณผ ์˜ˆ์ œ ์ฝ”๋“œ

์šฉ๋‡ฝ 2023. 10. 12. 18:03
๋ฐ˜์‘ํ˜•

์†Œ๊ฐœ

๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)์€ ๋Œ€ํ‘œ์ ์ธ ๋ถ„ํ•  ์ •๋ณต(Divede and Conquer) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜๋กœ, ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆˆ ๋’ค ๊ฐ๊ฐ์„ ์žฌ๊ท€์ ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ์ •๋ ฌ๋œ ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•ฉ์ณ์„œ ์ •๋ ฌ๋œ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

์ž‘๋™ ๊ฐœ๋…๐Ÿ’ก

๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ธ ๋‹จ๊ณ„๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

  1. ๋ถ„ํ• (Divide): ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ™์€ ํฌ๊ธฐ์˜ ๋‘ ๊ฐœ์˜ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋ถ„ํ• ์€ ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„ ์ง€์ ์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.
  2. ์ •๋ณต(Conquer): ๊ฐ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๋ณ‘ํ•ฉ์ •๋ ฌ์„ ์ด์šฉํ•ด ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ์ž…๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘์•„์งˆ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค.
  3. ๋ณ‘ํ•ฉ(Merge): ์ •๋ ฌ๋œ ๋‘ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • 0๊ฐœ ์š”์†Œ, 1๊ฐœ ์š”์†Œ ๋ฐฐ์—ด์ด ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค๋Š” ์ ์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด์„ ๋” ์ž‘์€ ๋ฐฐ์šธ๋ ค ๋‚˜๋ˆ„๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ ์•Œ๋ ค์ง„ ๋ฐฉ์‹์ด๊ณ , ๋” ํฐ ๋ฐฐ์—ด์„ ๋‚˜๋ˆ„๊ณ , ๋” ์ž‘์€ ํ•˜์œ„ ๋ฐฐ์—ด๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ 0 ๋˜๋Š” 1๊ฐœ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
  • ๊ณ„์† ๋ถ„ํ• ํ•œ ๋‹ค์Œ ๋‹ค์‹œ ๋ณ‘ํ•ฉ์‹œํ‚ต๋‹ˆ๋‹ค.

๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉํ•˜๋Š” ์ฝ”๋“œ

function merge(arr1, arr2) {
  const results = [];
  let i = 0;
  let j = 0;

  while (i < arr1.length && j < arr2.length) {
    if (arr2[j] > arr1[i]) {
      results.push(arr1[i]);
      i++;
    } else {
      results.push(arr2[j]);
      j++;
    }
  }

  while (i < arr1.length) {
    results.push(arr1[i]);
    i++;
  }

  while (j < arr2.length) {
    results.push(arr2[j]);
    j++;
  }

  return results;
}
  1. ์ž…๋ ฅ ๋‘ ๊ฐœ๋ฅผ ์ทจํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์—ฌ ๋นˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๋งˆ์ง€๋ง‰์— ๋ฐ˜ํ™˜ํ•  ๋นˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  2. ๊ฐ ์ž…๋ ฅ ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’(์ฒ˜์Œ)๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  3. i์™€ j ๋‘ ๊ฐœ์˜ ์นด์šดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์นด์šดํ„ฐ๋“ค์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  4. ์•„์ง ์‚ดํŽด๋ณด์ง€ ์•Š์€ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด, ์ฆ‰ i์™€ j๊ฐ€ ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด ๋์— ๋„๋‹ฌํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
    1. ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ฐ’์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ์ทจํ•œ ๋‹ค์Œ ๋‘ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ ๊ฐ’๊ณผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.
    2. ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์ด ๋” ์ž‘๋‹ค๋ฉด results ๋ฐฐ์—ด์— ๋„ฃ๊ณ  ๋‹ค์Œ ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๋‹ค์Œ ํ•ญ๋ชฉ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
    3. ๋ฐ˜๋Œ€๋กœ ์ฒซ ๋ฒˆ์งธ ๋ฐฐ์—ด์— ์žˆ๋Š” ํ•ญ๋ชฉ์ด ๋” ํฌ๋‹ค๋ฉด ๋‘ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ์ทจํ•˜์—ฌ results ๋ฐฐ์—ด์— ๋„ฃ์€ ๋‹ค์Œ ๋‘ ๋ฒˆ์งธ ๋ฐฐ์—ด์˜ ๋‹ค์Œ ๊ฐ’์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค
  5. ๋ฐฐ์—ด ํ•˜๋‚˜๋ฅผ ์™„๋ฃŒํ•œ ๋‹ค์Œ์—๋Š” ๋‹ค๋ฅธ ๋ฐฐ์—ด์˜ ๋‚จ์€ ๊ฐ’์„ ๋ชจ๋‘ ๋„ฃ์Šต๋‹ˆ๋‹ค.

๋ณ‘ํ•ฉ์ •๋ ฌ ๊ตฌํ˜„ ์ฝ”๋“œ

function mergeSort(arr) {
  if (arr.length <= 1) return arr;
  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));

  return merge(left, right);
}

console.log(mergeSort([10, 24, 76, 73])); // [10, 24, 73, 76]
  1. ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1๋˜๋Š” 0์ด ๋  ๋•Œ๊นŒ์ง€ mergeSort ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ฐฐ์—ด์˜ ์ ˆ๋ฐ˜์„ ์ž๋ฅด๊ธฐ ์œ„ํ•ด์„œ mid๋ผ๋Š” ๋ณ€์ˆ˜์— ๋ฐฐ์—ด ๊ธธ์ด / 2์˜ ๋‚ด๋ฆผ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  3. ์™ผ์ชฝ ์ ˆ๋ฐ˜๊ณผ ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜์„ slice ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ๋‚˜๋ˆ ์คฌ๋Š”๋ฐ, ์ด๋“ค์—๊ฒŒ mergeSort๋ฅผ ํ˜ธ์ถœํ•ด ์ค๋‹ˆ๋‹ค.

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

์ตœ๊ณ  ํ‰๊ท  ์ตœ์•… ๊ณต๊ฐ„ ๋ณต์žก๋„
O(n log n) O(n log n) O(n log n) O(n)

๊ณ„์† ๋‚˜๋ˆ„๊ณ  ๋‚˜๋ˆˆ ๋‹ค์Œ ํ•ฉ์น˜๊ณ  ๋˜ ํ•ฉ์น  ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ณ‘ํ•ฉ ์ •๋ ฌ์—์„œ๋Š” ์˜ˆ์™ธ ์ผ€์ด์Šค๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

 

O(log n)์€ ๋งŒ์•ฝ 32๊ฐœ์˜ ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด 2x2x2x2x2๋ผ๋Š” ์˜๋ฏธ์ธ๋ฐ, n log n์€ ๊ฐ ๋ถ„ํ• ๋งˆ๋‹ค, ๋ณ‘ํ•ฉํ•  ๋•Œ O(n)๋ฒˆ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

n์˜ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค๋ฉด, ๋ณ‘ํ•ฉ ์ •๋ ฌ์ด ์•„๋‹Œ ๋ณ‘ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๋Š” O(n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ–๊ฒŒ ๋˜๋Š”๋ฐ ๊ทธ๋ž˜์„œ ์ข…ํ•ฉ์ ์œผ๋กœ ๋ณด๋ฉด ๋ณ‘ํ•ฉ ์ •๋ ฌ์˜ Big O๋Š” O(n log n)์ด ๋ฉ๋‹ˆ๋‹ค.

์ •๋ฆฌํ•˜์ž๋ฉด log n์€ n์ฆ๊ฐ€์— ๋”ฐ๋ฅด๋Š” ๋ถ„ํ•  ์ˆ˜์ž…๋‹ˆ๋‹ค.

n์— ๋”ฐ๋ผ ๋ถ„ํ• ํ•  ํšŸ์ˆ˜๋ฅผ ๋งํ•˜๊ณ , ๋ฐฐ์—ด์ด ๋Š˜์–ด๋‚˜๋ฉด log n ๋น„์œจ๋กœ ๋Š˜์–ด๋‚œ๋‹ค.
๋ณ‘ํ•ฉ์„ ์‹ค์ œ๋กœ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด O(n) ๋น„๊ต๊ฐ€ ํ•„์š”ํ•˜๊ณ  ๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” ์ด O(n log n)์ด ๋ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•