본문으로 바로가기
반응형


 문제


https://programmers.co.kr/learn/courses/30/lessons/42576?language=cpp





sort algorithm 정리


Sort함수를 사용하기 위해서는 algorithm 헤더를 추가해야 합니다. std::sort는 기본적으로 오름차순 정렬을 수행합니다.


FORMAT

1
2
3
4
template <class RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);


sort 범위 : [fitst, last) 폐구간 first 개구간 last





오름차순 정렬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "iostream"
#include "algorithm"
using namespace std;
 
int main(void)
{
    int num[5= { 1030402060 };
 
    sort(num, num + 5);
    for (int i = 0; i < 5; i++)
        cout << num[i] << ' ';
 
    return 0;
}

 

sort(시작, 끝);


결과

1
10 20 30 40 60





오름차순 정렬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "iostream"
#include "algorithm"
using namespace std;
 
int main(void)
{
    int num[10= {10020103040206054515};
 
    sort(num + 5, num + 10);
    for (int i = 0; i < 10; i++)
        cout << num[i] << ' ';
 
    return 0;
}
 

sort(5번째 배열, 끝)인 경우에 대해서 알아보겠습니다.


결과

1
100 20 10 30 40 5 15 20 45 60


5번째 배열부터 10번째 배열까지만 오름차순 정렬이 되고 0번째부터 4번째는 정렬이 되지 않은 상태로 존재합니다.





내림차순 정렬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "iostream"
#include "algorithm"
using namespace std;
 
bool desc(int a, int b)
{
    return a > b;
}
 
int main(void)
{
    int num[10= {10020103040206054515};
 
    sort(num, num + 10, desc);
 
    for (int i = 0; i < 10; i++)
        cout << num[i] << ' ';
 
    return 0;
}



결과

1
100 60 45 40 30 20 20 15 10 5






객체 정렬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "iostream"
#include "vector"
#include "algorithm"
#include "string"
using namespace std;
 
class Bundle
{
public:
    string name;
    int val;
 
    Bundle(string name, int val){
        this->name = name;
        this->val = val;
    }
 
    bool operator < (const Bundle &a) const {
        return this->val < a.val;
    }
};
 
int main(void)
{
    vector<Bundle> v;
 
    v.push_back(Bundle("Onion"700));
    v.push_back(Bundle("Mushroom"200));
    v.push_back(Bundle("Carrot"600));
    v.push_back(Bundle("Pickle"300));
    v.push_back(Bundle("Bean"500));
 
    sort(v.begin(), v.end());
    
    for (int i = 0; i < v.size(); i++)
        cout << v[i].val << ' ' << v[i].name << endl;
 
    return 0;
}



결과

1
2
3
4
5
200 Mushroom
300 Pickle
500 Bean
600 Carrot
700 Onion





소스 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    
    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());
    
    for(int i = 0; i <= completion.size(); i++)
    {
        if(participant[i] != completion[i])
        {
            answer = participant[i];
            return answer;
        }
    }
    answer = participant[participant.size() - 1];
    
    return answer;
}
 


정렬 후 원소의 값이 다른 경우 완주하지 못한 선수입니다. 


participant[participant.size() - 1];

completion이 participant의 원소의 개수보다 1개가 더 많기 때문에 인덱스 조회가 끝나도 값이 나오지 않으면 마지막 인덱스가 완주하지 못한 선수입니다.



2020/01/11 - [프로그래밍] - 비트맵의 구조, 24비트 비트맵의 구조는 어떻게 될까?

2020/01/05 - [프로그래밍/C] - 이중 연결 리스트(Double linked list), 이중 원형 연결 리스트 예제

2019/12/22 - [프로그래밍/C] - 프로그래머스 2016년, 날짜에 따른 요일 구하기

2019/08/25 - [프로그래밍/C#] - C# 마우스 제어(클릭, 좌표 이동)

2019/06/20 - [프로그래밍] - Arabic 프로그래밍 규칙


반응형