백준 2751 수 정렬하기2

2021. 1. 15. 17:18· 알고리즘/백준(BOJ)
반응형

백준 2751 수 정렬하기2 문제는 시간복잡도와 관계가 있는 문제이다.

 

일반적인 버블 정렬과 삽입 정렬로는 처리 시간이 오래 걸리기 때문에 좀 더 빠른 알고리즘으로 처리해야한다.

 

그 중에 대표적으로 Heap 정렬이 있다.

 

아래 코드는 Heap 정렬 코드다.

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

void push_heap(std::vector<int>& heap, int Value) {
	heap.push_back(Value);

	int index = heap.size() - 1;

	while (index > 0 && heap[(index - 1) / 2] < heap[index]) {
		std::swap(heap[index], heap[(index - 1) / 2]);
		index = (index - 1) / 2;
	}
}

void pop_heap(std::vector<int>& heap) {
	heap[0] = heap.back();
	heap.pop_back();
	int here = 0;
	while (true) {
		int left = here * 2 + 1, right = here * 2 + 2;
		if (left >= heap.size()) break;
		int next = here;
		if (heap[next] < heap[left]) next = left;
		if (right < heap.size() && heap[next] < heap[right]) next = right;
		if (next == here) break;
		std::swap(heap[here], heap[next]);
		here = next;
	}
}

void sort_heap(std::vector<int>& heap) {
	for (int count = 1; count < heap.size(); count++) {
		std::swap(heap[heap.size() - count], heap[0]);
		int here = 0;
		while (true) {
			int left = here * 2 + 1, right = here * 2 + 2;
			if (left >= heap.size() - count) break;
			int next = here;
			if (heap[next] < heap[left]) next = left;
			if (right < heap.size() - count && heap[next] < heap[right]) next = right;
			if (next == here) break;
			std::swap(heap[here], heap[next]);
			here = next;
		}
	}
}

int main() {
	int N;
	std::cin >> N;

	std::vector<int> heap;
	for (int i = 0; i < N; i++) {
		int temp;
		std::cin >> temp;
		push_heap(heap, temp);
	}

	sort_heap(heap);

	for (auto content : heap)
		std::cout << content << "\n";

	return 0;
}
반응형

'알고리즘 > 백준(BOJ)' 카테고리의 다른 글

백준 10872 팩토리얼  (0) 2021.01.22
백준 10989 수 정렬하기 3  (0) 2021.01.17
백준 2750 수 정렬하기  (0) 2021.01.15
백준 10818 최소, 최대  (0) 2021.01.15
백준 2231번 분해합  (0) 2021.01.11
'알고리즘/백준(BOJ)' 카테고리의 다른 글
  • 백준 10872 팩토리얼
  • 백준 10989 수 정렬하기 3
  • 백준 2750 수 정렬하기
  • 백준 10818 최소, 최대
오뚜깅
오뚜깅
오뚜깅
오뚜깅
오뚜깅
전체
오늘
어제
  • 분류 전체보기
    • 취업인생
    • Programming
      • C & C++
      • Python
      • OpenCV
      • PCL
      • ROS
      • Deep learning
      • Network
    • 알고리즘
      • 이론
      • 백준(BOJ)
      • 프로그래머스(Programmers)
    • Project
    • IT
      • 우분투
    • 일상
      • 말씀 묵상
      • 끄적임
      • 영어 일기

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • cuda9.0
  • 우분투
  • cv_bridge
  • rospy
  • CuDNN
  • graphicdriver
  • cudaversion
  • PointCloudLibrary
  • installcudnn
  • 2292
  • opencv
  • 오츠알고리즘
  • c++code
  • installcuda
  • pytorch
  • C++
  • cuda설치
  • CUDA
  • tensorflowversion
  • imageclustering
  • 딥러닝환경구축
  • pointcloud
  • 백준2231
  • installubuntu
  • OtsuAlgorithm
  • kmeansclustering
  • 사용자지정정규화공식
  • 백준2798
  • DeepLearning
  • clustering

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
오뚜깅
백준 2751 수 정렬하기2
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.