선택 정렬 알고리즘
2019. 10. 10. 01:26ㆍ알고리즘/암기
선택 정렬이란 가장 작은 것을 선택해서 앞으로 보내는 정렬기법이다. 가장 작은 것을 선택하는 데에 N번과 리스트의 길이 N이 곱해져서 총 시간복잡도 O(N^2)을 갖게 된다
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <climits> | |
#include <iostream> | |
using namespace std; | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
vector<int> a(n); | |
for (int i = 0; i < n; i++) { | |
cin >> a[i]; | |
} | |
for (int i = 0; i < n - 1; i++) { | |
int minv = INT_MAX; | |
int index = i; | |
for (int j = i; j < n; j++) { | |
if (minv > a[j]) { | |
index = j; | |
minv = a[j]; | |
} | |
} | |
int tmp = a[i]; | |
a[i] = a[index]; | |
a[index] = tmp; | |
} | |
for (int i = 0; i < n; i++) { | |
cout << a[i] << ' '; | |
} | |
cout << '\n'; | |
return 0; | |
} |
'알고리즘 > 암기' 카테고리의 다른 글
stable sort와 unstable sort (0) | 2019.10.10 |
---|---|
삽입 정렬 알고리즘 (0) | 2019.10.10 |
내림차순 정렬된 배열을 뒤집기 (0) | 2019.10.09 |
퀵 정렬 알고리즘 (0) | 2019.10.08 |
문자열 탐색 KMP (0) | 2019.09.25 |