๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜-์ด๋ก ] ์‚ฝ์ž… ์ •๋ ฌ (Insertion Sort)

by Wonit 2019. 11. 23.

์‚ฝ์ž… ์ •๋ ฌ(Insertion Sort) ์ด๋ž€?

์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” i๊ฐœ ์งœ๋ฆฌ์˜ ๋ฐฐ์—ด์— ํ•˜๋‚˜์˜ ์›์†Œ๋ฅผ ๋” ๋”ํ•˜์—ฌ ์ •๋ ฌ๋œ i+1 ๊ฐœ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ A[n] ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋‹ค.

 

์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด ์ง€๊ธˆ๊นŒ์ง€ ํ–ˆ๋˜ ์„ ํƒ ์ •๋ ฌ ์ด๋‚˜ ๋ฒ„๋ธ” ์ •๋ ฌ์€ n ๊ฐœ์˜ ๋ฐฐ์—ด์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํ•˜๋‚˜์”ฉ ์›์†Œ๋ฅผ ์ค„์ด๋Š”๋ฐ ๋ฐ˜ํ•˜์—ฌ ์‚ฝ์ž… ์ •๋ ฌ์€ ๊ทธ ํฌ๊ธฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋Š˜๋ฆฌ๋Š” ์ •๋ ฌ์ด๋‹ค.

 

ํ•˜์ง€๋งŒ ์„ ํƒ ์ •๋ ฌ์ด๋‚˜ ๋ฒ„๋ธ” ์ •๋ ฌ์— ๋น„ํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๊ธฐ ์กฐ๊ธˆ ๊นŒ๋‹ค๋กญ๋‹ค๋Š” ์ ์ด ์žˆ๋‹ค.

 

๋ฐฑ๋ฌธ์ด ๋ถˆ์—ฌ์ผํƒ€ ๋ผ๊ณ  pseudo ์ฝ”๋“œ๋ฅผ ํ•œ ๋ฒˆ ๊ฐ€๋ณ๊ฒŒ ๋ณด๊ณ  ์ง์ ‘ ์ƒ๊ฐํ•ด๋ณด์ž.

insertionSort(A[], n){                    .... 1

    for i <- 2 to n                        .... 2

        A[1 ... i]์˜ ์ ํ•ฉํ•œ ์ž๋ฆฌ์— A[i]๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.    ....3

}
  • 1๋ฒˆ์˜ for ๋ฃจํ”„๋Š” ๋ฌธ์ œ์˜ ํฌ๊ธฐ๋ฅผ ํ•˜๋‚˜์”ฉ, ์ฆ‰ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค ์ƒˆ๋กญ๊ฒŒ ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.( ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์‹œ์ ์€ ํ•ญ์ƒ ์ •๋ ฌ์ด ๋˜์–ด์žˆ๋‹ค.)
  • 2๋ฒˆ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜๋ฉด A[1...i] ๊นŒ์ง€ ์ •๋ ฌ์ด ๋œ๋‹ค. ์ฆ‰ ์ด๋ฏธ ์ •๋ ฌ๋œ ์›์†Œ A[1...i-1]์— ์ƒˆ๋กญ๊ฒŒ A[i]๋ฅผ ์ถ”๊ฐ€์‹œํ‚ค๊ณ  ์—ฌ๊ธฐ์„œ ์กฐ๊ฑด์ด ์žˆ๋‹ค.
  • A[i] > A[i-1] ์ด๋ฉด A[i]๋Š” A[1..i-1] ์˜ ๋ชจ๋“  ์›์†Œ๋ณด๋‹ค ํฌ๋ฏ€๋กœ ๊ทธ๋ƒฅ ์ œ์ž๋ฆฌ์— ๋‘๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ A[i]๊ฐ€ ๋“ค์–ด๊ฐˆ ์ž๋ฆฌ๋ฅผ ์ฐพ์•„๊ฐ€๋ฉด ๋œ๋‹ค.
insertionSort(A[], n){
    for i <- 2 to n {
        loc <- i-1;
        newItem <- A[i];

        while(loc>=1 and newItem<A[loc]){
            A[loc+1] <- A[loc];
            loc --;
        }
        A[loc+1] <- newItem;
    }
}

  • ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด A[] = {7, 22, 31, 4, 11}์ด ์žˆ๋‹ค๊ณ  ์น˜์ž. ๊ฐ ์ •๋ ฌ์€ A[0] ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ์ ์ฐจ ์ธ๋ฑ์Šค๋ฅผ ํ•˜๋‚˜์”ฉ ๋Š˜๋ ค๊ฐ€๋ฉฐ ์ •๋ ฌ์„ ํ•  ๊ฒƒ์ด๋‹ค.

์ฒซ ๋ฒˆ์งธ ์ •๋ ฌ

  • ๋ฐฐ์—ด์˜ ์ƒํƒœ: {7}
๋ฐฐ์—ด์ธ๋ฑ์Šค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. -> A[i] = A[i+1] -> 22 ๊ฐ€ 7๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ -> ํฌ๋ฏ€๋กœ ๊ทธ ์ž๋ฆฌ ์œ ์ง€ : {7, 22}

๋ฐฐ์—ด์ธ๋ฑ์Šค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. -> A[i] = A[i+1] -> 31 ์ด 22๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ -> ํฌ๋ฏ€๋กœ ๊ทธ ์ž๋ฆฌ ์œ ์ง€ : {7, 22, 31}

๋ฐฐ์—ด์ธ๋ฑ์Šค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. -> A[i] = A[i+1] -> 4 ๊ฐ€ 31 ๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ -> ์ž‘์œผ๋ฏ€๋กœ ์›์†Œ ์œ„์น˜ ๊ฒ€์‚ฌ
ใ„ด> ๋ฐฐ์—ด 7์ด 4๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ -> ํฌ๋ฏ€๋กœ 7์˜ ์ž๋ฆฌ์— 4 ์‚ฝ์ž… : {4, 7, 22, 31}

๋ฐฐ์—ด์ธ๋ฑ์Šค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. -> A[i] = A[i+1] -> 11์ด 31 ๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ -> ์ž‘์œผ๋ฏ€๋กœ ์›์†Œ ์œ„์น˜ ๊ฒ€์‚ฌ
ใ„ด> ๋ฐฐ์—ด 4๊ฐ€ 11๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ -> ์ž‘์œผ๋ฏ€๋กœ ๋ฐฐ์—ด ์ธ๋ฑ์Šค ํ•œ ์นธ ๋œ€ -> 7์ด 11๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ-> ์ž‘์œผ๋ฏ€๋กœ ๋ฐฐ์—ด์ธ๋ฑ์Šค ํ•œ ์นธ ๋œ€ ->22๊ฐ€ 11๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌ -> ํฌ๋ฏ€๋กœ 22์ž๋ฆฌ์— 11 ์‚ฝ์ž… : {4, 7, 11, 22, 31}

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

์œ„์˜ pseudo ์ฝ”๋“œ์—์„œ ๋ณด๋ฉด ๋ฐ˜๋ณต์€ ์ด ๋‘ ๋ฒˆ ๋“ฑ์žฅํ•œ๋‹ค
1 ๋ฒˆ์˜ for๋ฌธ ๊ทธ๋ฆฌ๊ณ  3๋ฒˆ์— ์†ํ•ด์žˆ๋Š” while๋ฌธ. ์ž…๋ ฅ์˜ ํฌ๊ธฐ๊ฐ€ n ์ผ ๋•Œ ๋ฐ˜๋ณต์€ n ์— ๊ด€๋ จ์ด ์žˆ์œผ๋ฏ€๋กœ O(n^2)์˜ ์‹œ๊ฐ„์ด ๋“ ๋‹ค.

ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ๋ฐฐ์—ด์ด ์™„์ „ํžˆ ์ •๋ ฌ๋œ ์ฑ„๋กœ ์ž…๋ ฅ๋œ๋‹ค๋ฉด while ๋ฃจํ”„๋Š” ํ•œ ๋ฒˆ๋„ ์ˆ˜ํ–‰๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ O(n)์— ๊ฐ€๊นŒ์šด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ์ด๋Ÿฐ ๋งค๋ ฅ ๋ฅ๊ตฐ์— ๋‹ค๋ฅธ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฐ€๋” ์‚ฝ์ž…์ •๋ ฌ์„ ์„ž์–ด์„œ ์“ฐ๋Š” ๊ฒƒ๋„ ๊ถŒ์ž๋œ๋‹ค.


์‚ฝ์ž… ์ •๋ ฌ(Insertion Sort)์˜ ํŠน์ง•

์žฅ์ 

  • ์•ˆ์ „ํ•œ ์ •๋ ฌ ๋ฐฉ๋ฒ•์ด๋‹ค.
  • ๋ฐฐ์—ด์˜ ์ˆ˜๊ฐ€ ์ ์„ ์ˆ˜๋ก ๋‹ค๋ฅธ ์ •๋ ฌ ๋ฐฉ๋ฒ•๋ณด๋‹ค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๊ฐ€ ๊ฐ„๋‹จํ•˜๋ฏ€๋กœ ์œ ๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋ฏธ ๋ฐฐ์—ด์ด ์ •๋ ฌ๋˜์—ˆ์„ ๊ฒฝ์šฐ ๊ทน์ƒ์˜ ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์ 

  • ์‹œ๊ฐ„๋ณต์žก๋„์˜ ๊ด€์ ์—์„œ ๋ดค์„ ๋•Œ O(n^2)์ด๋ฏ€๋กœ ์ ผํ˜€ ํšจ์œจ์ ์ด์ง€ ์•Š๋‹ค.

์‚ฝ์ž… ์ •๋ ฌ(Insertion Sort)์˜ ์ž๋ฐ” ์ฝ”๋“œ

๋Œ“๊ธ€