[μκ³ λ¦¬μ¦] 02. μ ν μ λ ¬
μλ νμΈμ? μ£Όλνλ μ λλ€.
μ€λμ μλ£κ΅¬μ‘°μ μκ³ λ¦¬μ¦μ μκ³ λ¦¬μ¦μμ 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
π νκΈ° μλ£
π μκ³ λ¦¬μ¦ κ΅¬ν
π 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 }$ μ΄λλλ€!
μ£Όλνλμ κΈμ΄ λ§μμ λμ
¨λμ? ꡬλ
κ³Ό 곡κ°! κ·Έλ¦¬κ³ , λκΈμ μ£Όλνλμκ² λ§μ νμ΄ λ©λλ€