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

[์•Œ๊ณ ๋ฆฌ์ฆ˜-์ด๋ก ] ๋ฒ„๋ธ” ์ •๋ ฌ (Bubble Sort)

by Wonit 2019. 11. 23.

๋ฒ„๋ธ” ์ •๋ ฌ (Bubble Sort)์ด๋ž€?

 

๋ฒ„๋ธ” ์ •๋ ฌ์€ ์ธ์ ‘ํ•œ ๋‘ ์›์†Œ๋ฅผ ๋น„๊ตํ•˜์—ฌ ํฐ/์ž‘์€ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž๋ฆฌ๋ฅผ ๊ตํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋œปํ•œ๋‹ค.

 

์„ ํƒ์ •๋ ฌ๊ณผ ๊ฐ™์ด ์ œ์ผ ํฐ ์›์†Œ๋ฅผ ๋์ž๋ฆฌ๋กœ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋Š”๋ฐ ์ œ์ผ ํฐ ์›์†Œ๋ฅผ ์˜ฎ๊ธฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

 

๋ฒ„๋ธ”์ •๋ ฌ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด.

๋ผ์šด๋“œ์˜ ์ˆœ์„œ๋ณ„๋กœ ์ž„์˜์˜ ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋’ท ๊ฐ’๊ณผ ๋น„๊ต๋ฅผ ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋งŒ์•ฝ ๋’ท ๊ฐ’๋ณด๋‹ค ํฌ๊ฒŒ๋˜๋ฉด ์ž๋ฆฌ๋ฅผ ์˜ฎ๊ฒจ ๊ทธ ๋‹ค์Œ ๊ฐ’๊ณผ ๋น„๊ตํ•˜๊ณ  ํฌ์ง€ ์•Š๋‹ค๋ฉด ํฌ์ธํ„ฐ๋ฅผ ๋’ค๋กœ ๋„˜๊ฒจ ํฌ์ธํ„ฐ๊ฐ€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ๋„๋‹ฌํ–ˆ์„ ๋•Œ ํ•œ ๋ผ์šด๋“œ๊ฐ€ ์ข…๋ฃŒ๋œ๋‹ค.

 

pseudo ์ฝ”๋“œ๋ฅผ ๋จผ์ € ๋ณด์ž๋ฉด

bubbleSort(A[], n){                    .... 1
    for last <- n downto 2            .... 2
        for i <- i to last - 1        .... 3
            if (A[i] > A[i+1]) then A[i] <-> A[i+1]        .... 4 
}

  • ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด{7, 22, 31, 4, 11, 40}์ด ์žˆ๋‹ค๊ณ  ์น˜์ž. ๊ฐ ์ •๋ ฌ์€ ๋ผ์šด๋“œ๋ฅผ ํ†ตํ•ด ์ง„ํ–‰๋œ๋‹ค.

 

์ฒซ ๋ฒˆ์งธ ๋ผ์šด๋“œ

  • *: ์œ„์น˜ ํฌ์ธํ„ฐ
  • ์ตœ์ดˆ ํฌ์ธํ„ฐ ์œ„์น˜: {*7, 22, 31, 4, 11, 40}

7๊ณผ 22๋ฅผ ๋น„๊ตํ•œ๋‹ค -> 7๊ณผ 22์ค‘ 22๊ฐ€ ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 22๋กœ ์˜ฎ๊ธด๋‹ค. ๊ฒฐ๊ณผ: {7, *22, 31, 4, 11, 40}

22์™€ 31์„ ๋น„๊ตํ•œ๋‹ค -> 22์™€ 31์ค‘ 31์ด ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 31๋กœ ์˜ฎ๊ธด๋‹ค. ๊ฒฐ๊ณผ: {7, 22, *31, 4, 11, 40}

31๊ณผ 4๋ฅผ ๋น„๊ตํ•œ๋‹ค -> 31์™€ 4์ค‘ 31์ด ๋” ํฌ๋ฏ€๋กœ 4์™€ ์ž๋ฆฌ ๊ตํ™˜์„ ํ•œ๋‹ค. ๊ฒฐ๊ณผ: {7, 22, 4, *31, 11, 40}

31๊ณผ 11์„ ๋น„๊ตํ•œ๋‹ค -> 31๊ณผ 11์ค‘ 31์ด ๋” ํฌ๋ฏ€๋กœ 11๊ณผ ์ž๋ฆฌ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. ๊ฒฐ๊ณผ: {7, 22, 4, 11, *31, 40}

31๊ณผ 40์„ ๋น„๊ตํ•œ๋‹ค -> 31๊ณผ 40์ค‘ 40์ด ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 40์œผ๋กœ ์˜ฎ๊ธด๋‹ค. ๊ฒฐ๊ณผ: {7, 22, 4, 11, 31, *40}

[40 ์ •๋ ฌ ์™„๋ฃŒ]

ํฌ์ธํ„ฐ์˜ ์œ„์น˜๊ฐ€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์œผ๋ฏ€๋กœ 1๋ฒˆ์งธ ๋ผ์šด๋“œ๋ฅผ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

๋‘ ๋ฒˆ์งธ ๋ผ์šด๋“œ

  • *: ์œ„์น˜ ํฌ์ธํ„ฐ
  • ์ตœ์ดˆ ํฌ์ธํ„ฐ ์œ„์น˜: {*7, 22, 31, 4, 31}

7๊ณผ 22๋ฅผ ๋น„๊ตํ•œ๋‹ค -> 7๊ณผ 22์ค‘ 22๊ฐ€ ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 22๋กœ ์˜ฎ๊ธด๋‹ค. ๊ฒฐ๊ณผ: {7, *22, 4, 11, 31}

22์™€ 4๋ฅผ ๋น„๊ตํ•œ๋‹ค -> 22์™€ 4์ค‘ 22๊ฐ€ ๋” ํฌ๋ฏ€๋กœ 4์™€ ์ž๋ฆฌ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. ๊ฒฐ๊ณผ: {7, 4, *22, 11, 31}

22์™€ 11์„ ๋น„๊ตํ•œ๋‹ค -> 22์™€ 11์ค‘ 22๊ฐ€ ๋” ํฌ๋ฏ€๋กœ 11๊ณผ ์ž๋ฆฌ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. ๊ฒฐ๊ณผ: {7, 4, 11, *22, 31}

22์™€ 31์„ ๋น„๊ตํ•œ๋‹ค -> 22์™€ 31์ค‘ 31์ด ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 31๋กœ ์˜ฎ๊ธด๋‹ค ๊ฒฐ๊ณผ: {7, 4, 11, 22 ,*31}

[{31, 40} ์ •๋ ฌ ์™„๋ฃŒ]

ํฌ์ธํ„ฐ์˜ ์œ„์น˜๊ฐ€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์œผ๋ฏ€๋กœ 2๋ฒˆ์จฐ ๋ผ์šด๋“œ๋ฅผ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

์„ธ ๋ฒˆ์งธ ๋ผ์šด๋“œ

  • *: ์œ„์น˜ ํฌ์ธํ„ฐ
  • ์ตœ์ดˆ ํฌ์ธํ„ฐ ์œ„์น˜: {*7, 4, 11, 22}

7๊ณผ 4๋ฅผ ๋น„๊ตํ•œ๋‹ค -> 7๊ณผ 4์ค‘ 7์ด ๋” ํฌ๋ฏ€๋กœ 4์™€ ์ž๋ฆฌ๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. ๊ฒฐ๊ณผ: {4, *7, 11, 22}

7๊ณผ 11์„ ๋น„๊ตํ•œ๋‹ค -> 7๊ณผ 11์ค‘ 11์ด ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 11๋กœ ์˜ฎ๊ธด๋‹ค ๊ฒฐ๊ณผ: {4, 7 ,*11, 22}

11๊ณผ 22๋ฅผ ๋น„๊ตํ•œ๋‹ค -> 11๊ณผ 22์ค‘ 22๊ฐ€ ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 22๋กœ ์˜ฎ๊ธด๋‹ค ๊ฒฐ๊ณผ: {4, 7 ,11 ,*22}

[{11 22 31 40} ์ •๋ ฌ ์™„๋ฃŒ]

ํฌ์ธํ„ฐ์˜ ์œ„์น˜๊ฐ€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์œผ๋ฏ€๋กœ 3๋ฒˆ์จฐ ๋ผ์šด๋“œ๋ฅผ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

๋„ค ๋ฒˆ์งธ ๋ผ์šด๋“œ

  • *: ์œ„์น˜ ํฌ์ธํ„ฐ
  • ์ตœ์ดˆ ํฌ์ธํ„ฐ ์œ„์น˜: {*4, 7, 11}

4์™€ 7์„ ๋น„๊ตํ•œ๋‹ค -> 4์™€ 7์ค‘ 7์ด ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 7๋กœ ์˜ฎ๊ธด๋‹ค. ๊ฒฐ๊ณผ: {4, *7, 11}

7๊ณผ 11์„ ๋น„๊ตํ•œ๋‹ค -> 7๊ณผ 11์ค‘ 11์ด ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 11๋กœ ์˜ฎ๊ธด๋‹ค ๊ฒฐ๊ณผ: {4, 7 ,*11}

[{7 11 22 31 40} ์ •๋ ฌ ์™„๋ฃŒ]

ํฌ์ธํ„ฐ์˜ ์œ„์น˜๊ฐ€ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์œผ๋ฏ€๋กœ 4๋ฒˆ์จฐ ๋ผ์šด๋“œ๋ฅผ ์ข…๋ฃŒ์‹œํ‚จ๋‹ค.

๋งˆ์ง€๋ง‰ ๋ผ์šด๋“œ

  • *: ์œ„์น˜ ํฌ์ธํ„ฐ
  • ์ตœ์ดˆ ํฌ์ธํ„ฐ ์œ„์น˜: {*4, 7}

4์™€ 7์„ ๋น„๊ตํ•œ๋‹ค -> 4์™€ 7์ค‘ 7์ด ๋” ํฌ๋ฏ€๋กœ ํฌ์ธํ„ฐ๋ฅผ 7๋กœ ์˜ฎ๊ธด๋‹ค ๊ฒฐ๊ณผ: {4, *7}

[{4 7 11 22 31 4} ์ •๋ ฌ ์™„๋ฃŒ]


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

์œ„์˜ pseudo ์ฝ”๋“œ๋ฅผ ๋ดค์„ ๋•Œ ์ž…๋ ฅ ๋ฐฐ์—ด A[n]์ด๋ฉด (n์€ ์ž…๋ ฅ์˜ ํฌ๊ธฐ)

for ๋ฐ˜๋ณต

  • ...2 ๋Š” ์„ ํƒ์ •๋ ฌ์˜ for ๋ฃจํ”„์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜๊ณ  ๋ฃจํ”„๋ฅผ ๋Œ ๋•Œ๋งˆ๋‹ค ์ œ์ผ ํฐ ์›์†Œ๋ฅผ ๋งจ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ณด๋‚ด๊ณ  ์ •๋ ฌํ•  ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ํ•˜๋‚˜์”ฉ ์ค„์—ฌ๋‚˜๊ฐ„๋‹ค.

  • ...3 ๋Š” ์•ˆ์ชฝ ๋ฃจํ”„๋กœ์จ ๊ฐ€์žฅ ํฐ ์›์†Œ๋ฅผ ๋งจ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์ด ์„ ํƒ์ •๋ ฌ๊ณผ ๋‹ค๋ฅด๋‹ค. ์„ ํƒ์ •๋ ฌ์€ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ฐพ์€ ๋‹ค์Œ ๋งจ ์™ผ์ชฝ์œผ๋กœ ๋ณด๋‚ด๋Š” ๋ฐ˜๋ฉด, ๋ฒ„๋ธ”์ •๋ ฌ์€ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ด์›ƒํ•œ ์ˆ˜๋ฅผ ๋น„๊ตํ•ด๊ฐ€๋ฉฐ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค.

๊ฒฐ๋ก : ...2 ๋ฒˆ์˜ for ๋ฃจํ”„์—์„œ๋Š” n-1๋ฒˆ ์ˆœํ™˜ํ•˜๊ณ  ...3๋ฒˆ์˜ for ๋ฃจํ”„์—์„œ๋Š” last-1 ๋ฒˆ ์ˆœํ™˜ํ•˜๋Š”๋ฐ last๊ฐ€ 2๊นŒ์ง€ 1์”ฉ ๊ฐ์†Œํ•˜๋ฏ€๋กœ n(n-1)/2 ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒ„๋ธ”์ •๋ ฌ์˜ ์ˆ˜ํ–‰์‹œ๊ฐ„์€ ∂(n^2)์ด๋‹ค.
Best Avg Worst
n^2 n^2 n^2

๋ฒ„๋ธ” ์ •๋ ฌ (Bubble Sort)์˜ ํŠน์ง•

์žฅ์ 

  • ๊ตฌํ˜„์ด ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค.

๋‹จ์ 

  • ์ˆœ์„œ์— ๋งž์ง€ ์•Š์€ ์š”์†Œ๋ฅผ ์ธ์ ‘ํ•œ ์š”์†Œ์™€ ๊ตํ™˜ํ•œ๋‹ค.
  • ํ•˜๋‚˜์˜ ์š”์†Œ๊ฐ€ ๊ฐ€์žฅ ์™ผ์ชฝ์—์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐฐ์—ด์—์„œ ๋ชจ๋“  ๋‹ค๋ฅธ ์š”์†Œ๋“ค๊ณผ ๊ตํ™˜๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ํŠนํžˆ ํŠน์ • ์š”์†Œ๊ฐ€ ์ตœ์ข… ์ •๋ ฌ ์œ„์น˜์— ์ด๋ฏธ ์žˆ๋Š” ๊ฒฝ์šฐ๋ผ๋„ ๊ตํ™˜๋˜๋Š” ์ผ์ด ์ผ์–ด๋‚œ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ ๊ตํ™˜์ด ์ด๋™๋ณด๋‹ค ๋” ๋ณต์žกํ•˜๊ธฐ๋•Œ๋ฌธ์— ๊ฑฐ์˜ ์“ฐ์ง€ ์•Š๋Š”๋‹ค

๋ฒ„๋ธ” ์ •๋ ฌ์˜ ์ตœ๊ณ  ํšจ์œจ

์•ž์—์„œ ์†Œ๊ฐœํ•œ ๋ฒ„๋ธ”์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ˆ˜ํ–‰์„ ์‹œ์ž‘ํ•  ๋•Œ๋‚˜ ์ค‘๊ฐ„์— ๋ฐฐ์—ด์ด ์ด๋ฏธ ์ •๋ ฌ์ด๋˜์–ด ์žˆ๋Š” ์ƒํƒœ๋ผ๋„ ๊ณ„์† ๋๊นŒ์ง€ ๋ฌด์˜๋ฏธํ•œ ์ˆœํ™˜์„ ๊ณ„์†ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํ•œ ๊ฐ€์ง€ ์žฅ์น˜์ธจ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

bubbleSort(A[], n){
    for last <- 2{                    .... 1
        sorted <- true;
        for i <- 1 to last - 1;{        .... 2
               if(A[i]>A[i+1]) then{        
                A[i] <-> A[i+1];        .... 3
                sorted <- false;        .... 4
            }
        }
        if(sorted = true) then return;        .... 5
    }

}
sorted <- true; ์ด ๋ถ€๋ถ„์„ sorted Flag๋ผ๊ณ  ์นญํ•˜๊ณ  ํ”Œ๋ž˜๊ทธ๊ฐ€ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ...2์˜ ๋ฐ˜๋ณต๋ฌธ์„ ๋™์ž‘์‹œ์ผœ์•ผ ํ•œ๋‹ค.
์ฆ‰ ์ •๋ ฌ์ด ์ด๋ฃจ์–ด์ง€๋ฉด ์ž๋™์ ์œผ๋กœ sorted ํ”Œ๋ž˜๊ทธ๊ฐ€ ๋™์ž‘ํ•˜์—ฌ ํ”Œ๋ž˜๊ทธ ๊ฐ’์„ false๋กœ ๋ฐ”๊ฟ”๋†“๊ณ  ๋งŒ์•ฝ ....2์˜ ๋ฐ˜๋ณต๋ฌธ์ด ๋™์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ”๋กœ return ํ•˜๊ฒŒ ๋œ๋‹ค.

 


๋ฒ„๋ธ” ์ •๋ ฌ(Bubble Sort)์˜ ์ž๋ฐ” ์ฝ”๋“œ

๋Œ“๊ธ€