[์•Œ๊ณ ๋ฆฌ์ฆ˜] 02. ์„ ํƒ ์ •๋ ฌ

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

728x90
๋ฐ˜์‘ํ˜•

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

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

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



 


 

 

 

 

๐Ÿ“Œ Selection Sort (์„ ํƒ ์ •๋ ฌ)๋ž€?


์„ ํƒ ์ •๋ ฌ์€ ์ฃผ์–ด์ง„ Data ์ค‘ ์ตœ์†Œ๊ฐ’์„ ์ฐพ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฐ ๋’ค ํ•ด๋‹น ์ตœ์†Œ๊ฐ’์„ ๋งจ ์•ž์— ์œ„์น˜ํ•œ ๊ฐ’๊ณผ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ๋งจ ์•ž์˜ ์œ„์น˜๋ฅผ ๋บ€ ๋‚˜๋จธ์ง€ Data๋ฅผ ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์ด๋ฏธ ์ตœ์†Œ๊ฐ’์œผ๋กœ ์ž๋ฆฌ๊ฐ€ ์ด๋™ ๋œ ๊ฒƒ๋“ค์€ ๋‹ค์‹œ ์ž๋ฆฌ ์ด๋™์„ ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋‹ˆ ์ •๋ ฌ์˜ ์ˆ˜๋Š” n -1 ๋งŒํผ ์ค„์–ด๋“œ๋Š” ๊ฒƒ์ด์—์š”.

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ : 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/Selection_sort

 

 

๐Ÿ“Œ ํ•„๊ธฐ ์ž๋ฃŒ


 

 

 

 

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


    ๐Ÿ“ Python

for stand in range(len(data_list) - 1 # ๋กœ ๋ฐ˜๋ณต
	lowest = stand					  # ๋กœ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”
    
    for num in range(stand, len(data_list)	# stand ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ๊ธธ์ด (๋ฐฐ์—ด์˜ ๊ธธ์ด) - 1๊นŒ์ง€ ๋ฐ˜๋ณต
    	๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ data_list[lowest] > data_list[num]	# ์ด๋ฉด,
        	lowest = num	#	๋น„๊ต ๋Œ€์ƒ ๊ธฐ์ค€ Index์— ๋น„๊ต ๋Œ€์ƒ Index๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
            
    data_list[num], data_list[lowest] data_list[lowest], data_list[num]	# ํ•ด๋‹น ์œ„์น˜์˜ ๊ฐ’๋“ค์„ ์œ„์น˜ ์ด๋™ํ•œ๋‹ค.

 

 

 

๐Ÿ“Œ ๋น…์˜ค ํ‘œ๊ธฐ๋ฒ•์— ์˜ํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„


๋ฐ˜๋ณต๋ฌธ์ด ๋‘๊ฐœ ์ด๊ณ , ๊ทธ ์•ˆ์—์„œ data_list์˜ ๊ธธ์ด๊ฐ€ ์˜ํ–ฅ์„ ์ฃผ๊ธฐ์— O($n^2$) ์ธ ๊ฒƒ์ด์—์š”.

์กฐ๊ธˆ ๋” ๊นŠ๊ฒŒ ์ƒ์„ธํ•˜๊ฒŒ ๊ณ„์‚ฐ์„ ํ•œ๋‹ค๋ฉด $\frac { n * (n - 1)}{ 2 }$ ์ด๋ž๋‹ˆ๋‹ค!

 

 


 

 

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

 

728x90
๋ฐ˜์‘ํ˜•