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

 

+ Recent posts