wn42
코딩이랑 이것저것
wn42
전체 방문자
오늘
어제
  • 분류 전체보기 (113)
    • 프로그래머스 (23)
      • LV1 (11)
      • LV2 (1)
      • LV3 (3)
      • 연습 (8)
    • 딥러닝 공부 (0)
      • 머신러닝&딥러닝 이론 (0)
    • 임베디드 (17)
      • Adventure Design (1)
      • 센서기반모바일로봇 (5)
      • ROS (9)
      • Google Coral (2)
    • C++ (38)
      • C++ 기초 (34)
      • 자료구조 및 알고리즘 (4)
    • Python (14)
      • 기본 파이썬 문법 (6)
      • Python 기초 (8)
    • 빅데이터 (9)
      • 빅데이터 첫걸음 시작하기(국비지원) (5)
      • 빅데이터 공부 (4)
    • 알고리즘 공부 (2)
      • 기본 알고리즘 (2)
    • 전자공학 (10)
      • 반도체 공정 (3)
      • 무선데이터통신 (7)
      • 반도체공학 (0)
    • C# (0)
      • C# 기본 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 데이터분석 인강
  • 클래스
  • 바이트디그리
  • 빅데이터 첫걸음 시작하기
  • c++
  • Queue
  • 내일배움카드
  • 변수
  • Python
  • 패스트캠퍼스
  • stl
  • 노드
  • ROS
  • 인스턴스
  • 조건문
  • 파이썬
  • 빅데이터
  • K디지털크레딧
  • 데이터분석
  • 스택
  • numpy
  • 상속
  • 반복문
  • 딥러닝
  • google coral
  • 스택/큐
  • 큐
  • 정렬
  • 프로그래머스
  • 소멸자

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
wn42

코딩이랑 이것저것

C++/자료구조 및 알고리즘

[C++] 정렬 알고리즘

2022. 11. 23. 13:18

Sort() 함수

  • C++의 algorithm 헤더에 포함되어 있음

<오름차순>

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    vector<int> num_list = {2, 1, 5, 4, 3};
    sort(begin(num_list), end(num_list));
    for (int i=0; i<num_list.size(); i++){
      cout << num_list[i] << " ";
    }
    return 0;
}
1 2 3 4 5

 

<내림차순>

1. 함수 이용

  • 함수를 추가하면 해당 함수의 반환 값에 맞게 정렬을 할 수 있음을 이용한다.
  • 왼쪽의 숫자가 오른쪽 숫자보다 크도록 함수 설정
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool compare(int a, int b){
	return a > b;
}

int main() {
    vector<int> num_list = {2, 1, 5, 4, 3};
    sort(begin(num_list), end(num_list), compare);
    for (int i=0; i<num_list.size(); i++){
      cout << num_list[i] << " ";
    }
    return 0;
}
5 4 3 2 1

 

2. rbegin, rend 이용

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    vector<int> num_list = {2, 1, 5, 4, 3};
    sort(rbegin(num_list), rend(num_list));
    for (int i=0; i<num_list.size(); i++){
      cout << num_list[i] << " ";
    }
    return 0;
}
5 4 3 2 1

 

<특정 변수 기준 정렬>

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Student {
public:
    string name;
    int score;
    Student(string name, int score){
        this->name = name;
        this->score = score;
    }
    bool operator < (Student &student){
        return this->score < student.score;
    }
};

int main() {
    Student students[] = {
        Student("Steven", 11),
        Student("Daniel", 15),
        Student("Kane", 8)
    };
    sort(students, students + 3);
    cout << length << endl;
    for (int i=0; i<3; i++){
        cout << students[i].name << " ";
    }
    return 0;
}
Kane Steven Daniel

(참고: https://blog.naver.com/ndb796/221227975229)

 

 

    'C++/자료구조 및 알고리즘' 카테고리의 다른 글
    • [C++] STL 자료구조
    • [C++] String
    • [C++] Vector Container
    wn42
    wn42
    코딩이랑 이것저것 하는 블로그

    티스토리툴바