YeJin's Footsteps

SelectionSort(선택 정렬) 본문

Computer Science & Engineering/알고리즘

SelectionSort(선택 정렬)

YeJinii 2021. 4. 16. 15:17
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

//선택정렬
template <class T> //template <typename T>
void selectSort(T * arr, const int size){
    for(int i=0; i<size-1; i++){
        int j=i;
        for(int k=i+1; k<size; k++){
            if(arr[j]>arr[k]){
                j=k;
            }
        }
        swap(arr[j],arr[i]);
    }
}

template <class T>
void printArray(T * arr, const int size){
    for(int i=0; i<size; i++){
        cout<<arr[i]<<' ';
    }
    cout<<'\n';
}

int main(void) {
    
    int arr[10]={1,3,4,2,5,9,0,8,7,6};
    char carr[10]={'a','c','h','b','d','j','e','f','g','i'};
    
    printArray(arr, sizeof(arr)/sizeof(int));
    selectSort(arr, sizeof(arr)/sizeof(int));
    printArray(arr, sizeof(arr)/sizeof(int));
    
    printArray(carr, sizeof(carr));
    selectSort(carr, sizeof(carr)); 
    printArray(carr, sizeof(carr));
    
    return 0;
}

'Computer Science & Engineering > 알고리즘' 카테고리의 다른 글

튜플  (0) 2021.05.02
크레인 인형 뽑기 게임  (0) 2021.05.02
완주하지 못한 선수  (0) 2021.05.02
next_permutation(start, end)  (0) 2021.04.18
겨울 방학_백준 문제 풀이(아이패드 기록)  (0) 2021.04.04
Comments