ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

์„ ํƒ ์ •๋ ฌ:

์ด n๊ฐœ์˜ ์›์†Œ๊ฐ€ ์žˆ์„ ๋•Œ,

0๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ n-1 ๋ฒˆ์งธ ์›์†Œ๊นŒ์ง€

i ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์›์†Œ๊นŒ์ง€ ์ตœ์†Œ๊ฐ’์„ ๊ฐ–๋Š” ์›์†Œ์™€ i ๋ฒˆ์งธ ์›์†Œ๋ฅผ swapํ•ด์ฃผ๋Š” ์ •๋ ฌ.

 

๋ฒ„๋ธ” ์ •๋ ฌ๊ณผ ๋‹ฌ๋ฆฌ ํ•œ Cycle ๋‚ด์—์„œ ํ•œ ๋ฒˆ๋งŒ swap(๊ตํ™˜)์ด ์ผ์–ด๋‚œ๋‹ค.

 

์‹œ๊ฐ„๋ณต์žก๋„: Θ(n^2)

 

์„ ํƒ ์ •๋ ฌ

 

 

[5, 2, 6, 3, 1, 4] ์„ ํƒ ์ •๋ ฌ

 

 

์„ ํƒ ์ •๋ ฌ Python ์ฝ”๋“œ

def selectionSort():
    nums = list(map(int, input().split(' ')))
    
    for i in range(0, len(nums)):
        min_num = nums[i]
        min_idx = i
        
        for j in range(i+1, len(nums)):
            if(nums[j] < min_num):
                min_num = nums[j]
                min_idx = j
        nums[min_idx] = nums[i]
        nums[i] = min_num
    	print(nums)
        
    print(nums)

์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ(Step by Step)

 

728x90
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ
250x250