๋ฐ˜์‘ํ˜•

๋ถ„ํ•  ์ •๋ณต (Devide & Conquer)


๊ฐœ๋…

๋ถ„ํ•  ์ •๋ณต์€ “ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ ๋‹จ์œ„๋กœ ์ชผ๊ฐœ๋ฉด์„œ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹"์ด๋‹ค. ๋ฏธ๊ตญ ์ˆ˜ํ•™์ž ํฐ๋…ธ์ด๋งŒ์ด 1945๋…„์— ๊ฐœ๋ฐœํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ํ€ต์†ŒํŠธ, ๋ณ‘ํ•ฉ ์ •๋ ฌ์ด ๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•œ๋‹ค.

์˜ˆ์ „์—” ํ…Œ์ดํ”„๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ–ˆ๋‹ค. ํ…Œ์ดํ”„ ๋“œ๋ผ์ด๋ธŒ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋Š” ํ•ญ์ƒ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฝ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ ์–ด๋ ค์› ๋‹ค. ์ด๋Ÿฐ ํ…Œ์ดํ”„ ๋“œ๋ผ์ด๋ธŒ์˜ ๋ฌธ์ œ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ๋ณ‘ํ•ฉ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํƒ„์ƒํ–ˆ๋‹ค.

 

  1. ๋ถ„ํ•  : ๋ฌธ์ œ๋ฅผ ๋” ์ด์ƒ ๋ถ„ํ• ํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋™์ผ ์œ ํ˜•์˜ ์—ฌ๋Ÿฌ ํ•˜์œ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆˆ๋‹ค
  2. ์ •๋ณต : ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„์˜ ํ•˜์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉฐ ์ •๋ณตํ•œ๋‹ค
  3. ์กฐํ•ฉ : ํ•˜์œ„ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์›๋ž˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋กœ ์กฐํ•ฉํ•œ๋‹ค

 

์˜ˆ์‹œ

๋ถ„ํ•  ์ •๋ณต์€ ๊ตฌ์กฐ๋Š” ๋™์ผํ•˜์ง€๋งŒ ๋” ์ž‘์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ์‹์ด ๋ฐ˜๋ณต๋˜๋Š” ์žฌ๊ท€๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 5์˜ ๊ณ„์Šน(5!)์„ ์žฌ๊ท€๋กœ ๊ณ„์‚ฐํ•˜๋Š” factorial ํ•จ์ˆ˜๊ฐ€ ์ „ํ˜•์ ์ธ ๋ถ„ํ•  ์ •๋ณต์˜ ์˜ˆ๋‹ค. 5์˜ ๊ณ„์Šน์€ 5 × 4 × 3 × 2 × 1์˜ ๊ฒฐ๊ณผ์ด๋ฏ€๋กœ 5๋ถ€ํ„ฐ -1์”ฉ ๋ฌธ์ œ๋ฅผ ์ชผ๊นฌ ํ›„(๋ถ„ํ• ), ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„์˜ ํ•˜์œ„ ๋ฌธ์ œ์ธ 2 × 1 ๋ถ€ํ„ฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉฐ(์ •๋ณต/์กฐํ•ฉ) ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•œ๋‹ค.

 

  • $5 \times (4 \times 3 \times 2 \times 1) = 5 \times \text{fact}(5-1) = (5 \times 24)$
  • $4 \times (3 \times 2 \times 1) = 4 \times \text{fact}(4-1) = (4 \times 6)$
  • $3 \times (2 \times 1) = 3 \times \text{fact}(3-1) = (3 \times 2)$
  • $2 \times 1 = 2 \times \text{fact}(2-1) = (2 \times 1)$
  • $1\,\text{(Base Case)}$

 

function fac(n) {
  // Base Case ์žฌ๊ท€๊ฐ€ ๋ฉˆ์ถ”๋Š” ์กฐ๊ฑด
  if (n === 1) return 1;
  
  // Recursive Case
  return n * fac(n - 1);
}

fac(5); // 120

 

๋ณ‘ํ•ฉ ์ •๋ ฌ


์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…

โžŠ, โž‹ ๊ฐ™์€ ์›ํ˜• ๋ฒˆํ˜ธ๋Š” ์ง„ํ–‰ ์ˆœ์„œ / ํŒŒ๋ž€์ƒ‰ ๋ธ”๋ก์€ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋“ค์ด ์ •๋ ฌ๋œ ์ƒํƒœ

๋ณ‘ํ•ฉ ์ •๋ ฌ์€ 1๊ฐœ ๋ฐฐ์—ด์„ โžŠ2๊ฐœ์˜ ์ž‘์€ ๋ฐฐ์—ด๋กœ ๋ถ„ํ• ํ•˜์—ฌ โž‹๊ฐ ๋ฐฐ์—ด์„ ์ •๋ ฌ(์ •๋ณต)ํ•œ ํ›„ โžŒ๋‹ค์‹œ ๋ณ‘ํ•ฉ(๊ฒฐํ•ฉ)ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

  1. ๋ถ„ํ•  : ์ž…๋ ฅ ๋ฐ›์€ ๋ฐฐ์—ด์„ ๋™์ผ ๊ธธ์ด์˜ 2๊ฐœ ๋ฐฐ์—ด๋กœ ๋ถ„ํ• 
  2. ์ •๋ณต : ๋ถ„ํ• ํ•œ ๋ฐฐ์—ด ์ •๋ ฌ. ๋ถ„ํ• ํ•œ ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 2 ์ด์ƒ์ด๋ฉด ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ๋ถ„ํ•  ์ •๋ณต ๋‹ค์‹œ ์ ์šฉ
  3. ๊ฒฐํ•ฉ : ์ •๋ ฌํ•œ 2๊ฐœ์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ๋ณ‘ํ•ฉ

 

๊ตฌํ˜„

๐Ÿ’ก ๋ณ‘ํ•ฉ ์ •๋ ฌ ๊ตฌํ˜„์€ ํฌ๊ฒŒ ๋ถ„ํ•  ๋‹จ๊ณ„ / ๋ณ‘ํ•ฉ ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค

 

์œ ํ‹ธ ํ•จ์ˆ˜

const swap = (arr, a, b) => {
  /* const temp = arr[a];
  arr[a] = arr[b];
  arr[b] = temp; */
  [arr[a], arr[b]] = [arr[b], arr[a]];
};

const Compare = { LESS_THAN: -1, BIGGER_THAN: 1 };

const defaultCompare = (a, b) => {
  if (a === b) return 0;
  return a < b ? Compare.LESS_THAN : Compare.BIGGER_THAN;
};

 

  • swap : ๋ฐฐ์—ด๊ณผ a, b ์ธ๋ฑ์Šค๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ๋ฐฐ์—ด[a], ๋ฐฐ์—ด[b] ์š”์†Œ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜
  • Compare : ๊ฐ’ ๋น„๊ต์‹œ ์‚ฌ์šฉํ•˜๋Š” ์ƒ์ˆ˜.
  • defaultCompare : a, b ๊ฐ’์„ ์ธ์ž๋กœ ๋ฐ›์•„ ์–ด๋Š ๊ฐ’์ด ๋” ํฐ์ง€ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜

 

๋ณ‘ํ•ฉ

left, right 2๊ฐœ์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ(๋ฐฐ์—ด)๋ฅผ ๋ฐ›์•„ ๋ณ‘ํ•ฉํ•˜๋Š” ํ•จ์ˆ˜. ๋‘ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ 0๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋น„๊ตํ•ด์„œ ๋” ์ž‘์€ ๊ฐ’์„ result ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค. left ํ˜น์€ right์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ result ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ–ˆ๋‹ค๋ฉด, ๋‚จ์€ ์š”์†Œ๋ฅผ result ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ ํ›„ ์ข…๋ฃŒํ•œ๋‹ค.

const merge = (left, right, compare) => {
  let i = 0; // left ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค
  let j = 0; // right ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค
  const result = [];
  // left ํ˜น์€ right์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ result ๋ฐฐ์—ด์— ์ถ”๊ฐ€๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
  while (i < left.length && j < right.length) {
    const isLeft = compare(left[i], right[j]) === Compare.LESS_THAN;
    result.push(isLeft ? left[i++] : right[j++]);
  }
  // ๋‚จ์€ ์š”์†Œ๋ฅผ result ๋ฐฐ์—ด์— ์ถ”๊ฐ€
  return result.concat(i < left.length ? left.slice(i) : right.slice(j));
};

 

merge ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ด๋ฏธ “์ •๋ ฌ”๋œ ๋‘ ๋ถ„ํ•  ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ณผ์ •

 

๋ถ„ํ• 

๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 1์ด ๋ ๋•Œ๊นŒ์ง€ ์ ˆ๋ฐ˜์”ฉ(left, right) ๋ถ„ํ• ํ•œ ํ›„, merge ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ถ„ํ• ํ•œ ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ(์ž‘์€ ์ˆซ์ž๋ถ€ํ„ฐ)์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด์„œ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋Š” ํ•จ์ˆ˜.

const mergeSort = (arr, compare = defaultCompare) => {
  if (arr.length > 1) {
    // ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 1์ด ๋ ๋•Œ๊นŒ์ง€ ๋ฐฐ์—ด ๋ถ„ํ• 
    const { length } = arr;
    const middle = Math.floor(length / 2); // [๋ถ„ํ• ] | [5, 4, 3, 2, 1] -> middle: 2, ...
    const left = mergeSort(arr.slice(0, middle)); // [์ •๋ณต] | [5, 4], ...
    const right = mergeSort(arr.slice(middle, length)); // [์ •๋ณต] | [3, 2, 1], ...
    arr = merge(left, right, compare); // [๊ฒฐํ•ฉ]
  }
  return arr;
};
๋”๋ณด๊ธฐ
[21, 10, 12, 20, 25, 13, 15, 22] ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉ ์ •๋ ฌํ•˜๋Š” ๊ณผ์ •

๋ถ„ํ•  / ์ •๋ณต โ–ผ

f([21, 10, 12, 20, 25, 13, 15, 22])
===================================
middle = 4
โ‘ด left = f([21, 10, 12, 20]) | [L-1] -> [10, 12, 20, 21]
โ‘พ right = f([25, 13, 15, 22]) | [R-4] -> [13, 15, 22, 25]
ใ‰‘ arr = merge([10, 12, 20, 21], [13, 15, 22, 25]) | [M-7] -> [10, 12, 13, 15, 20, 21, 22, 25]
---------------------------------------
return [10, 12, 13, 15, 20, 21, 22, 25]

[L-1] f([21, 10, 12, 20])
=========================
middle = 2
โ‘ต left = f([21, 10]) | [L-2] -> [10, 21]
โ‘น right = f([12, 20]) | [R-2] -> [12, 20]
โ‘ฝ arr = merge([10, 21], [12, 20]) | [M-3] -> [10, 12, 20, 21]
-----------------------
return [10, 12, 20, 21]

[L-2] f([21, 10])
=================
middle = 1
โ‘ถ left = f([21]) -> [L-3] -> [21]
โ‘ท right = f([10]) -> [R-1] -> [10]
โ‘ธ arr = merge([21], [10]) | [M-1] -> [10, 21]
---------------
return [10, 21]

[R-2] f([12, 20])
=================
middle = 1
โ‘บ left = f([12]) -> [L-4] -> [12]
โ‘ป right = f([20]) -> [R-3] -> [20]
โ‘ผ arr = merge([12], [20]) | [M-2] -> [12, 20]
---------------
return [12, 20]

[R-4] f([25, 13, 15, 22])
=========================
middle = 2
โ‘ฟ left = f([25, 13]) | [L-5] -> [13, 25]
โ’ƒ right = f([15, 22]) | [R-6] -> [15, 22]
โ’‡ arr = merge([13, 25], [15, 22]) | [M-6] -> [13, 15, 22, 25]
-----------------------
return [13, 15, 22, 25]

[L-5] f([25, 13])
=================
middle = 1
โ’€ left = f([25]) -> [L-6] -> [25]
โ’ right = f([13]) -> [R-5] -> [13]
โ’‚ arr = merge([25], [13]) | [M-4] -> [13, 25]
---------------
return [13, 25]

[R-6] f([15, 22])
=================
middle = 1
โ’„ left = f([15]) -> [L-7] -> [15]
โ’… right = f([22]) -> [R-7] -> [22]
โ’† arr = merge([15], [22]) -> [M-5] -> [15, 22]
---------------
return [15, 22]

 

๋ณ‘ํ•ฉ โ–ผ

[M-1] merge([21], [10])
=======================
i = 0 / j = 0 -> 21 vs 10 -> [10] | i++
---------------
return [10, 21]

[M-2] merge([12], [20])
=======================
i = 0 / j = 0 -> 12 vs 20 -> [12] | i++
---------------
return [12, 20]

[M-3] merge([10, 21], [12, 20])
===============================
i = 0 / j = 0 -> 10 vs 12 -> [10] | i++
i = 1 / j = 0 -> 21 vs 12 -> [10, 12] | j++
i = 1 / j = 1 -> 21 vs 20 -> [10, 12, 20] | j++
-----------------------
return [10, 12, 20, 21]

[M-4] merge([25], [13])
=======================
i = 0 / j = 0 -> 25 vs 13 -> [13] | j++
---------------
return [13, 25]

[M-5] merge([15], [22])
=======================
i = 0 / j = 0 -> 15 vs 22 -> [15] | i++
---------------
return [15, 22]

[M-6] merge([13, 25], [15, 22])
===============================
i = 0 / j = 0 -> 13 vs 15 -> [13] | i++
i = 1 / j = 0 -> 25 vs 15 -> [13, 15] | j++
i = 1 / j = 1 -> 25 vs 22 -> [13, 15, 22] | j++
-----------------------
return [13, 15, 22, 25]

[M-7] merge([10, 12, 20, 21], [13, 15, 22, 25])
===============================================
i = 0 / j = 0 -> 10 vs 13 -> [10] | i++
i = 1 / j = 0 -> 12 vs 13 -> [10, 12] | i++
i = 2 / j = 0 -> 20 vs 13 -> [10, 12, 13] | j++
i = 2 / j = 1 -> 20 vs 15 -> [10, 12, 13, 15] | j++
i = 2 / j = 2 -> 20 vs 22 -> [10, 12, 13, 15, 20] | i++
i = 3 / j = 2 -> 21 vs 22 -> [10, 12, 13, 15, 20, 21] | i++
---------------------------------------
return [10, 12, 13, 15, 20, 21, 22, 25]

 

๋กœ๊ทธ(Logarithm) ์ฐธ๊ณ  ๋‚ด์šฉ

์‹œ๊ฐ„ ๋ณต์žก๋„์—์„œ ๋กœ๊ทธ๋Š” ๋ฐ‘(base)์„ ํ‘œ๊ธฐํ•˜์ง€ ์•Š์ง€๋งŒ, ๋ฐ‘์ด 2์ธ ๋กœ๊ทธ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค(logโ‚‚N).

 

  • ๋กœ๊ทธ๋Š” ์ง€์ˆ˜(exponent)์˜ ์ •๋ฐ˜๋Œ€ ๊ฐœ๋…. 2๋ฅผ ๋ช‡ ๋ฒˆ ๊ฑฐ๋“ญ์ œ๊ณฑํ•ด์•ผ 32๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ์ง€์ˆ˜, 32๋Š” 2๋ฅผ ๋ช‡ ๋ฒˆ ๋‚˜๋ˆ ์•ผ 1์ด ๋‚˜์˜ค๋Š”์ง€ ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ๋กœ๊ทธ. ์ฐธ๊ณ ๋กœ ๋กœ๊ทธ๋Š” ์ง€์ˆ˜ ๋ฐฉ์ •์‹์œผ๋กœ ๋ฐ”๊ฟ”์„œ ๊ณ„์‚ฐํ•˜๋ฉด ํŽธํ•จ(logโ‚(b)a๋ฅผ ๋ช‡ ๋ฒˆ ์ œ๊ณฑํ•ด์•ผ b๊ฐ€ ๋‚˜์˜ฌ์ง€ ๊ณ„์‚ฐํ•ด๋ณธ๋‹ค).
    • ์ง€์ˆ˜ : 2โฟ = 32 → 32๋Š” 2๋ฅผ 5๋ฒˆ ๊ฑฐ๋“ญ์ œ๊ณฑํ•ด์•ผ ๋‚˜์˜ค๋ฏ€๋กœ n = 5
    • ๋กœ๊ทธ : logโ‚‚(32) = n → 32๋Š” 2๋ฅผ 5๋ฒˆ ๋‚˜๋ˆ ์•ผ 1์ด ๋‚˜์˜ค๋ฏ€๋กœ n = 5
  • ์ฆ‰, O(logN)์€ ๋ฐ์ดํ„ฐ ์›์†Œ๊ฐ€ N๊ฐœ ์žˆ์„ ๋•Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด logโ‚‚N ๋‹จ๊ณ„๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค๋Š” ์˜๋ฏธ. ์š”์†Œ๊ฐ€ 8๊ฐœ๋ผ๋ฉด logโ‚‚8 = 3 ์ด๋ฏ€๋กœ O(logN) ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ 3๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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

๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ํฌ๊ฒŒ ๋ถ„ํ•  ๋‹จ๊ณ„์™€ ๋ณ‘ํ•ฉ ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. ๋ถ„ํ•  ๋‹จ๊ณ„์—์„  ๋น„๊ต / ์ด๋™ ์—ฐ์‚ฐ(์ž„์‹œ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ–ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ)์„ ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

  • Worst Case(์ •๋ ฌ์ด ์•ˆ๋์„ ๋•Œ) : O(nlogโ‚‚n)
  • Best Case(์ด๋ฏธ ์ •๋ ฌ๋์„ ๋•Œ) : O(nlogโ‚‚n)

 

๋ณ‘ํ•ฉ ๋‹จ๊ณ„์˜ ์ˆ˜ (์žฌ๊ท€ ํ˜ธ์ถœ์˜ ๊นŠ์ด)

๋”๋ณด๊ธฐ
์ด๋ฏธ์ง€ ๋ฐ ์„ค๋ช… ์ถœ์ฒ˜ - gmlwjd9405
  • ๋ ˆ์ฝ”๋“œ์˜ ๊ฐœ์ˆ˜ n์ด 2์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ n = 2^k ์ด๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ
  • n = 2^3์€  2^3 2^2 2^1 2^0 ์ˆœ์œผ๋กœ ์ค„์–ด๋“ค์–ด ์ˆœํ™˜ ํ˜ธ์ถœ์˜ ๊นŠ์ด๊ฐ€ 3์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค
  • ์œ„๋ฅผ ํ† ๋Œ€๋กœ n = 2^k์„ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด k(k = logโ‚‚n)์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค → k = logโ‚‚n

$k = logโ‚‚n$ (๋ฐ์ดํ„ฐ ๊ธธ์ด n์ด 8์ด๋ผ๊ณ  ๊ฐ€์ •, 8์€ 2๋ฅผ 3๋ฒˆ ์ œ๊ณฑํ•ด์•ผ ๋‚˜์˜ค๋ฏ€๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ์˜ ๊นŠ์ด k๋Š” 3)

 

๋ณ‘ํ•ฉ ์ •๋ ฌ ๋ฐ ํ€ต ์ •๋ ฌ์€ ๋ฐ์ดํ„ฐ ๊ธธ์ด๊ฐ€ 1์ด ๋ ๋•Œ๊นŒ์ง€ ์ ˆ๋ฐ˜์”ฉ ๋ถ„ํ• ํ•œ๋‹ค. ๋ฐ์ดํ„ฐ ๊ธธ์ด๊ฐ€ 8์ด๋ผ๋ฉด 4(8/2) → 2(4/2) → 1(2/2) ๋ถ„ํ•  ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฏ€๋กœ ๊นŠ์ด๋Š” 3์ด ๋œ๋‹ค. [21, 10, 12, 20, 25, 13, 15, 22] ๋ฐฐ์—ด์„ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด…

 

  • 1 depth : 8 / 2 = 4n / 2 ํฌ๊ธฐ์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด 1์Œ ๋น„๊ต (๋ถ€๋ถ„ ๋ฐฐ์—ด ๊ฐœ์ˆ˜ 2๊ฐœ)
    • ( [21, 10, 12, 20] — L
    • [25, 13, 15, 22] )โ‘  — R
  • 2 depth : 4 / 2 = 2n / 4 ํฌ๊ธฐ์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด 2์Œ ๋น„๊ต (๋ถ€๋ถ„ ๋ฐฐ์—ด ๊ฐœ์ˆ˜ 4๊ฐœ)
    • ( [21, 10] [12, 20] )โ‘  — L
    • ( [25, 13] [15, 22] )โ‘ก — R
  • 3 depth : 2 / 2 = 1n / 8 ํฌ๊ธฐ์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด 4์Œ ๋น„๊ต (๋ถ€๋ถ„ ๋ฐฐ์—ด ๊ฐœ์ˆ˜ 8๊ฐœ)
    • ( [21] [10] )โ‘  ( [12] [20] )โ‘ก — L
    • ( [25] [13] )โ‘ข ( [15] [22] )โ‘ฃ — R

 

๋ณ‘ํ•ฉ ๋‹จ๊ณ„์˜ ํฌ๊ธฐ ๋น„๊ต ์—ฐ์‚ฐ

๊ฐ ๋ณ‘ํ•ฉ ๋‹จ๊ณ„(์žฌ๊ท€ ํ˜ธ์ถœ์˜ ๊นŠ์ด)๋งˆ๋‹ค ์ตœ๋Œ€ n๋ฒˆ(๋ฐ์ดํ„ฐ ๊ธธ์ด)์˜ ๋น„๊ต ์—ฐ์‚ฐ ์ˆ˜ํ–‰์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

์•„๋ž˜๋Š” [21, 10, 12, 20, 25, 13, 15, 22] ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์˜ˆ์‹œ.

 

  • 3 depth : ๊ธธ์ด๊ฐ€ 1์ธ(n/8) ๋ถ€๋ถ„ ๋ฐฐ์—ด 4์Œ ๋น„๊ต
    • ๊ธธ์ด๊ฐ€ 1์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด 2๊ฐœ(1์Œ) ๋ณ‘ํ•ฉ์‹œ ์ตœ๋Œ€ 2๋ฒˆ(1×2)์˜ ๋น„๊ต ์—ฐ์‚ฐ ํ•„์š”
    • ๊ธธ์ด๊ฐ€ 1์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด์ด 8๊ฐœ(4์Œ)์ด๋ฏ€๋กœ 4(์Œ)×2(์—ฐ์‚ฐ)=8(๋ฒˆ) ์—ฐ์‚ฐ ํ•„์š”
  • 2 depth : ๊ธธ์ด๊ฐ€ 2์ธ(n/4) ๋ถ€๋ถ„ ๋ฐฐ์—ด 2์Œ ๋น„๊ต
    • ๊ธธ์ด๊ฐ€ 2์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด 2๊ฐœ(1์Œ) ๋ณ‘ํ•ฉ์‹œ ์ตœ๋Œ€ 4๋ฒˆ(2×2)์˜ ๋น„๊ต ์—ฐ์‚ฐ ํ•„์š”
    • ๊ธธ์ด๊ฐ€ 2์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด์ด 4๊ฐœ(2์Œ)์ด๋ฏ€๋กœ 2(์Œ)×4(์—ฐ์‚ฐ)=8(๋ฒˆ) ์—ฐ์‚ฐ ํ•„์š”
  • 1 depth : ๊ธธ์ด๊ฐ€ 4์ธ(n/2) ๋ถ€๋ถ„ ๋ฐฐ์—ด 1์Œ ๋น„๊ต
    • ๊ธธ์ด๊ฐ€ 4์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด 2๊ฐœ(1์Œ) ๋ณ‘ํ•ฉ์‹œ ์ตœ๋Œ€ 8๋ฒˆ(4×2)์˜ ๋น„๊ต ์—ฐ์‚ฐ ํ•„์š”
    • ๊ธธ์ด๊ฐ€ 4์ธ ๋ถ€๋ถ„ ๋ฐฐ์—ด์ด 2๊ฐœ(1์Œ)์ด๋ฏ€๋กœ 1(์Œ)×8(์—ฐ์‚ฐ)=8(๋ฒˆ) ์—ฐ์‚ฐ ํ•„์š”

 

๊ฐ ๋ณ‘ํ•ฉ ๋‹จ๊ณ„(depth)๋งˆ๋‹ค ์ตœ๋Œ€ n๋ฒˆ์˜ ๋น„๊ต ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š”…
๊ฐ ๋ณ‘ํ•ฉ ๋‹จ๊ณ„์˜ ๋น„๊ต ์—ฐ์‚ฐ(n) × ๋ณ‘ํ•ฉ ๋‹จ๊ณ„์˜ ์ˆ˜(logโ‚‚n) = nlogโ‚‚n
๋ฐ์ดํ„ฐ ๊ธธ์ด๊ฐ€ 8์ด๋ผ๋ฉด ์ตœ๋Œ€ 24๋ฒˆ์˜ ์ž‘์—… ์ˆ˜ํ–‰

 

์žฅ๋‹จ์ 

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

 

  1. ์žฅ์ 
    • ์ตœ์„  / ์ตœ์•… ๋ชจ๋‘ ๋™์ผํ•œ ์‹œ๊ฐ„ ์†Œ์š”(์–ด๋–ค ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋“  ์ •๋ ฌ ์‹œ๊ฐ„ ๋™์ผ / ๋ฐ์ดํ„ฐ ๋ถ„ํฌ ์˜ํ–ฅ ๋œ๋ฐ›์Œ)
    • Stable ์ •๋ ฌ ๋ฐฉ์‹
  2. ๋‹จ์ 
    • In-place ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์•„๋‹ˆ๋ฏ€๋กœ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ํ•„์š”
    • ๋ฐ์ดํ„ฐ ์–‘์ด ๋งŽ์•„์งˆ ์ˆ˜๋ก ์ด๋™ ํšŸ์ˆ˜ ๋Š˜์–ด๋‚จ → ๋ฌธ์ œ ํ•ด๊ฒฐ ์‹œ๊ฐ„ ์ฆ๊ฐ€

 

๋ ˆํผ๋Ÿฐ์Šค


 


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

 

๋ฐ˜์‘ํ˜•