Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Xilinx
- 정처기
- linux
- Zynq
- boj
- verilog HDL
- vitis
- Beakjoon
- Bus
- hdl
- FPGA
- baekjoon
- chip2chip
- 자격증
- C++
- 코딩테스트
- HDLBits
- 정보처리기사
- SQL
- axi
- amba
- UNIX
- 백준
- AMBA BUS
- Vivado
- 리눅스
- Backjoon
- 실기
- verilog
- java
Archives
- Today
- Total
Hueestory
[2751] 수 정렬하기 2 (C++) 본문
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
static vector<int> A;
static vector<int> result;
void merge(int s, int e);
void partition(int s, int e);
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n; cin >> n;
A.resize(n);
result.resize(n);
for (int i = 0; i < n; i++)
cin >> A[i];
partition(0, n - 1);
for (int i = 0; i < n; i++)
cout << A[i] << "\n";
return 0;
}
void partition(int s, int e)
{
int mid;
if (s < e) {
mid = (s + e) / 2;
partition(s, mid);
partition(mid + 1, e);
merge(s, e);
}
}
void merge(int s, int e)
{
int mid = (s + e) / 2;
int i = s;
int j = mid + 1;
int k = s;
while (i <= mid && j <= e) {
if (A[i] > A[j])
result[k++] = A[j++];
else
result[k++] = A[i++];
}
if (i > mid) {
while (j <= e)
result[k++] = A[j++];
}
else {
while (i <= mid)
result[k++] = A[i++];
}
for (int a = s; a <= e; a++)
A[a] = result[a];
}
1. vector A와 result를 크기 n으로 초기화
2. 병합 정렬 알고리즘을 사용
3. partition 함수에서 A를 나누어지지 않을 때까지 나눔
4. merge에서 나눠진 vector의 가장 앞 항부터 차례로 비교하여 result에 정렬
5. result에 정리된 값들을 A에 복사 후 출력
'PS(중단) > BOJ' 카테고리의 다른 글
[2023] 신기한 소수 (C++) (1) | 2024.05.01 |
---|---|
[10989] 수 정렬하기 3 (C++) (0) | 2024.05.01 |
[2884] 알람 시계 (C++) (0) | 2024.04.26 |
[2753] 윤년 (C++) (0) | 2024.04.25 |
[2741] N 찍기 (C++) (0) | 2024.04.25 |
Comments