😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with Python]_21_선택정렬 λ³Έλ¬Έ

πŸ¦₯ μ½”ν…Œ/이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with python

[이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with Python]_21_선택정렬

μ§•μ§•μ•ŒνŒŒμΉ΄ 2022. 2. 8. 00:38
728x90
λ°˜μ‘ν˜•

220208 μž‘μ„±

<λ³Έ λΈ”λ‘œκ·ΈλŠ” γ€Žμ΄κ²ƒμ΄ 취업을 μœ„ν•œ μ½”λ”© ν…ŒμŠ€νŠΈλ‹€γ€ μ˜ youtubeλ₯Ό μ°Έκ³ ν•΄μ„œ κ³΅λΆ€ν•˜λ©° μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€>

https://www.youtube.com/watch?v=jpyslMwprao&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=21 

 

 

 

 

 

 

1. μ •λ ¬ 

: 데이터λ₯Ό νŠΉμ •ν•œ 기쀀에 따라 μˆœμ„œλŒ€λ‘œ λ‚˜μ—΄ν•˜λŠ” 것

 

 

 

 

 

2. 선택 μ •λ ¬

: μ²˜λ¦¬λ˜μ§€ μ•Šμ€ 데이터 μ€‘μ—μ„œ κ°€μž₯ μž‘μ€ 데이터λ₯Ό 선택해 맨 μ•žμ— μžˆλŠ” 데이터와 λ°”κΎΈλŠ” 것

 

1) κ°€μž₯ μž‘μ€ 0 선택해 κ°€μž₯ μ•žμ˜ μˆ«μžμ™€ λ°”κΎΈκΈ° 

2) κ·Έλ‹€μŒ μž‘μ€ 1 선택해 κ°€μž₯ μ•žμ—μ„œ λ‘λ²ˆμ§Έ μˆ«μžμ™€ λ°”κΎΈκΈ°

....

이런 κ³Όμ • 반볡

 

  • python
array = [7, 5, 9, 0, 3, 1, 6, 2, 4, 8]

for i in range(len(array)) :
    min_index = i
    for j in range(i+1, len(array)) :
        if array[min_index] > array[j] :
            min_index = j
    
    array[i], array[min_index] = array[min_index], array[i]

print(array)

  • c++
#include <bits/stdc++.h>

using namespace std;

int n = 10;
int arr[10] = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};

int main(void) {
    for (int i = 0; i < n; i++) {
        int min_index = i; // κ°€μž₯ μž‘μ€ μ›μ†Œμ˜ 인덱슀 
        for (int j = i + 1; j < n; j++) {
            if (arr[min_index] > arr[j]) {
                min_index = j;
            }
        }
        swap(arr[i], arr[min_index]); // μŠ€μ™€ν”„
    }
    for(int i = 0; i < n; i++) {
        cout << arr[i] << ' ';
    }
}
  • java
import java.util.*;

public class Main {

    public static void main(String[] args) {

        int n = 10;
        int[] arr = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};

        for (int i = 0; i < n; i++) {
            int min_index = i; // κ°€μž₯ μž‘μ€ μ›μ†Œμ˜ 인덱슀 
            for (int j = i + 1; j < n; j++) {
                if (arr[min_index] > arr[j]) {
                    min_index = j;
                }
            }
            // μŠ€μ™€ν”„
            int temp = arr[i];
            arr[i] = arr[min_index];
            arr[min_index] = temp;
        }

        for(int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}

 

 

 

 

 

3. 선택 μ •λ ¬ μ‹œκ°„ λ³΅μž‘λ„

: N 번 만큼 κ°€μž₯ μž‘μ€ 수λ₯Ό μ°Ύμ•„μ„œ 맨 μ•žμœΌλ‘œ 보내야함

: N + (N-1) + (N-2) + (N-3) ... + 2

=> (N^2 + N - 2) / 2

=> O(N^2)

 

 

 

 

 

728x90
λ°˜μ‘ν˜•
Comments