[ Code Kata ] ๐Ÿคฏ python #21 ์„ ํƒ์ •๋ ฌ(Selection Sort)

Selection Sort(์„ ํƒ์ •๋ ฌ)

์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๐Ÿ‘‰๐Ÿป ์ˆœ์„œ๊ฐ€ ์—†๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ”๊พธ์–ด ๋‚˜์—ดํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
์ •๋ ฌ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ ์ค‘์— ์œ ๋ช…ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ์„ ํƒ์ •๋ ฌ
  • ๋ฒ„๋ธ”์ •๋ ฌ
  • ์‚ฝ์ž…์ •๋ ฌ
  • ํ€ต์ •๋ ฌ

์„ ํƒ์ •๋ ฌ ?

๐Ÿ‘‰๐Ÿป ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ ์ค‘, ๊ฐ€์žฅ ์ž‘์€ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜์—ฌ ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ด ๋‚˜๊ฐ€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

์˜ˆ์ œ๋ฅผ ๋ณด์ž๋ฉด,

์ •๋ ฌ์„ ํ•ด์•ผํ•˜๋Š” ๋ฐฐ์—ด
๐Ÿ‘‰๐Ÿป [7,5,4,2]

  • ์ฒซ ๋ฒˆ์งธ loop์—์„œ๋Š” index 0๋ถ€ํ„ฐ 3๊นŒ์ง€ ํ™•์ธํ•˜๋ฉฐ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
    2์ด๋ฏ€๋กœ index 0์˜ 7๊ณผ ๊ต์ฒดํ•ฉ๋‹ˆ๋‹ค. -> [2,5,4,7]
  • ๋‘ ๋ฒˆ์งธ๋Š” index 1๋ถ€ํ„ฐ 3๊นŒ์ง€ ํ™•์ธํ•˜๋ฉฐ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
    4์ด๋ฏ€๋กœ index 1์˜ 5์™€ ๊ต์ฒดํ•ฉ๋‹ˆ๋‹ค -> [2,4,5,7]
  • ์„ธ ๋ฒˆ์งธ๋Š” index 2๋ถ€ํ„ฐ 3๊นŒ์ง€.. ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์„ ํƒํ•ด์„œ ์ˆœ์„œ๋Œ€๋กœ ๊ต์ฒดํ•˜๋Š” ๊ฒƒ์„ ์„ ํƒ์ •๋ ฌ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ

nums๋ผ๋Š” ์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ˆซ์ž ๋ฐฐ์—ด์„ ์ฃผ๋ฉด, ์˜ค๋ฆ„์ฐจ์ˆœ(1,2,3..10) ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ returnํ•˜๊ธฐ

model solution

def selectionSort(nums):

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ