[์•Œ๊ณ ๋ฆฌ์ฆ˜] 01. ๋ฒ„๋ธ”์ •๋ ฌ

2021. 9. 7. 08:00ใ†์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์ค€๋น„/์•Œ๊ณ ๋ฆฌ์ฆ˜

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ Bubble Sort์— ๋Œ€ํ•ด ๊ณต๋ถ€ ํ•ด ๋ณด๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์†Œ์Šค ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ ํ™•์ธ ํ•˜๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค๊ป˜์„œ๋Š” ์ฃผ๋‹ˆํ•˜๋ž‘์˜ Git hub์— ๊ด€์‹ฌ์„ ์ฃผ์„ธ์š”!

 

 


 

 

๐Ÿ“Œ Sorting (์ •๋ ฌ)์ด๋ž€?


์ •๋ ฌ์€ ์–ด๋–ค Data๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ์ •ํ•ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์ •๋ ฌ์€ Program ์ž‘์„ฑ ์‹œ ๋นˆ๋ฒˆํ•˜๊ฒŒ ํ•„์š”ํ•˜๋‹ต๋‹ˆ๋‹ค! ๋•Œ๋ฌธ์— ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กด์žฌํ•˜๊ณ , ์ด๋ฅผ ๊ณต๋ถ€ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

๋‹ค์–‘ํ•œ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ดํ•ดํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋™์ผํ•œ ๋ฌธ์ œ์— ๋Œ€ํ•ด ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๊ณ , ๊ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ„์˜ ์„ฑ๋Šฅ ๋น„๊ต๋ฅผ ํ†ตํ•ด, ์„ฑ๋Šฅ ๋ถ„์„์— ๋Œ€ํ•ด์„œ๋Š” ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

 

 

๐Ÿ“Œ Bubble Sort (๋ฒ„๋ธ” ์ •๋ ฌ)๋ž€?


๋‘ ์ธ์ ‘ํ•œ Data๋ฅผ ์„œ๋กœ ๋น„๊ตํ•ด์„œ ์•ž์— ์žˆ๋Š” Data๊ฐ€ ๋’ค์— ์žˆ๋Š” Data๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ธ ๊ฒƒ์ด์—์š”.

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ : https://visualgo.net/ko/sorting

 

VisuAlgo - Sorting (Bubble, Selection, Insertion, Merge, Quick, Counting, Radix)

VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook, Twitter

visualgo.net

 

 

์ถœ์ฒ˜ : https://en.wikipedia.org/wiki/Bubble_sort

 

 

 

 

 

 

 

์ฃผ๋‹ˆํ•˜๋ž‘ ํ•„๊ธฐ๋ณธ

 

 

    ๐Ÿ“ Data๊ฐ€ ๋„ค ๊ฐœ ์ผ๋•Œ

data_list = [1, 9, 3, 2]
  • 1์ฐจ Round
    • 1๊ณผ 9 ๋น„๊ต, 1์ด ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™ ํ•„์š”์—†์Œ
      ํ˜„์žฌ ๊ฐ’ [1, 9, 3, 2]
    • 9์™€ 3์„ ๋น„๊ต, 9๊ฐ€ 3๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™
      ํ˜„์žฌ ๊ฐ’ [1, 3, 9, 2]
    • 9์™€ 2๋ฅผ ๋น„๊ต, 9๊ฐ€ 2๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™
      ํ˜„์žฌ ๊ฐ’ [1, 3, 2, 9]
  • 2์ฐจ Round
    • 1๊ณผ 3์„ ๋น„๊ต, 1์ด ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™ ํ•„์š”์—†์Œ
      ํ˜„์žฌ ๊ฐ’ [1, 3, 2, 9]
    • 3๊ณผ 2๋ฅผ ๋น„๊ต, 3์ด ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™
      ํ˜„์žฌ ๊ฐ’ [1, 2, 3, 9]
    • 3๊ณผ 9๋ฅผ ๋น„,๊ต 3์ด ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™ ํ•„์š”์—†์Œ
      ํ˜„์žฌ ๊ฐ’ [1, 2, 3, 9]
  • 3์ฐจ Round
    • 1๊ณผ 2๋ฅผ ๋น„๊ต, 1์ด ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™ ํ•„์š”์—†์Œ
      ํ˜„์žฌ ๊ฐ’ [1, 2, 3, 9]
    • 2์™€ 3์„ ๋น„๊ต, 2๊ฐ€ ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™ ํ•„์š”์—†์Œ
      ํ˜„์žฌ ๊ฐ’ [1, 2, 3, 9]
      3๊ณผ 9๋ฅผ ๋น„๊ต, 3์ด ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ž๋ฆฌ ์ด๋™ ํ•„์š”์—†์Œ
      ํ˜„์žฌ ๊ฐ’ [1, 2, 3, 9]

 

 

 

 

 

 

๐Ÿ“Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„


    ๐Ÿ“ ๋ฐ˜๋ณต๋˜๋Š” ๊ทœ์น™ ์ฐพ๊ธฐ

           ๐Ÿ›Ž n๊ฐœ์˜ List๊ฐ€ ์žˆ์„ ๋•Œ, ์ตœ๋Œ€ n-1๋ฒˆ์˜ Logic์ด ์ ์šฉ
           ๐Ÿ›ŽRound๋ฅผ 1๋ฒˆ์”ฉ ๊ฑฐ์น  ๋•Œ ๋งˆ๋‹ค ๊ฐ€์žฅ ํฐ ์ˆซ์ž๊ฐ€ ๋’ค์—์„œ๋ถ€ํ„ฐ 1๊ฐœ์”ฉ ์ž๋ฆฌ๋ฅผ ์žก๋Š”๋‹ค.
           ๐Ÿ›Ž์œ„์˜ ์ด์œ ๋กœ Round๊ฐ€ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ์ผ์ฐ ๋๋‚  ์ˆ˜๋„ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ Round๋ฅผ ์ ์šฉํ•  ๋•Œ, ํ•œ ๋ฒˆ๋„ ์•ž๊ณผ ๋’ค ์‚ฌ์ด ๋ฐ์ดํ„ฐ
                 ๊ตํ™˜์ด ์—†์—ˆ๋‹ค๋ฉด ์ด๋ฏธ ์ •๋ ฌ๋œ ์ƒํƒœ์ด๋ฏ€๋กœ ๋” ์ด์ƒ ๋กœ์ง์„ ๋ฐ˜๋ณต ์ ์šฉํ•  ํ•„์š” ์—†๋‹ค.

 

 

    ๐Ÿ“ Python ํ˜•์‹์œผ๋กœ ์ ์–ด๋ณด๊ธฐ

for num in range(len(data_list)):	# ๋ฐ˜๋ณต
	swap = False	# Data ๊ตํ™˜์ด ์ด๋ค„์กŒ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”
    
    for num1 in range(len(data_list) - num - 1): # ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ n -1 ๋ฒˆ ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—
    	# ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ ์•ž์— Index์˜ ๊ฐ’์ด ๋’ค์— Index ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด?
        if data_list[num1] > data_list[num1 + 1]: 
        	# ์„œ๋กœ Data ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค.
        	data_list[num1], data_list[num1 + 1] = data_list[num1 + 1], data_list[num1]
            
            # ํ•œ๋ฒˆ์ด๋ผ๋„ Data ๊ตํ™˜์ด ์ด๋ค„์กŒ๋‹ค๋ฉด if๋ฌธ์— ๋“ค์–ด์™”์„ ๊ฒƒ์ด๊ณ , swap๋ณ€์ˆ˜๋ฅผ True๋กœ ๋ฐ”๊พผ๋‹ค.
            swap = True		
            
    if swap == False:		# ํ•œ Round๋ฅผ ๋„๋Š”๋ฐ, Data์˜ ๊ตํ™˜์ด ํ•œ๋ฒˆ๋„ ์ด๋ค„์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด
    	break				# ์ด๋ฏธ Data๊ฐ€ ์ •๋ ฌ๋˜์—ˆ๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.

 

 


 

 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค

728x90
๋ฐ˜์‘ํ˜•