https://www.acmicpc.net/problem/10814
백준 10814번: 나이순 정렬
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<int, string> a, pair<int, string> b)
{
return a.first < b.first;
}
int main()
{
int num;
int age;
string name;
vector<pair<int, string>> members;
cin >> num;
for (int i = 0; i < num; i++)
{
cin >> age >> name;
members.push_back({ age, name });
}
stable_sort(members.begin(), members.end(), compare);
for (int i = 0; i < num; i++)
{
cout << members[i].first << ' ' << members[i].second << "\n";
}
}
stable_sort 대신 그냥 sort를 쓰게 되면,
algorithm 헤더의 기본 sort는 quicksort를 사용하여, 정렬 과정에서 입력된 순서가 보장되지 않을 수 있음
정렬 관련 참고 > https://code-lab1.tistory.com/24
[알고리즘] 기본 정렬 알고리즘 비교| stable vs not stable| in-place vs not in-place | 선택 정렬(selection sort)
정렬 알고리즘이란? 정렬 알고리즘은 n개의 숫자가 주어졌을 때 이를 사용자가 지정한 기준에 맞게 정렬하는 알고리즘이다. 아주 간단한 알고리즘부터 조금 복잡한 알고리즘까지, 여러가지 알
code-lab1.tistory.com
'백준' 카테고리의 다른 글
[백준] 1620번: 나는야 포켓몬 마스터 이다솜 (0) | 2023.01.30 |
---|---|
[백준] 2108번: 통계학 (0) | 2023.01.08 |
[백준] 4948번: 베르트랑 공준 (0) | 2022.12.19 |
[백준] 1193번: 분수찾기 (C++) (0) | 2022.12.08 |
[백준] 1018번: 체스판 다시 칠하기 (C++) (0) | 2022.03.29 |