https://www.acmicpc.net/problem/2751
#include <iostream>
#include <cstdio>
using namespace std;
int partition(int list[], int left, int right) {
int low, high, pivot;
low = left;
high = right + 1;
pivot = list[left];
do {
do {
low++;
} while (low <= right && list[low] < pivot);
do {
high--;
} while (high >= left && list[high] > pivot);
if (low < high) {
int temp = list[low];
list[low] = list[high];
list[high] = temp;
}
} while (low < high);
int temp = list[left];
list[left] = list[high];
list[high] = temp;
return high;
}
int* quick_sort(int list[], int left, int right) {
if (left < right) {
int q = partition(list, left, right);
quick_sort(list, left, q - 1);
quick_sort(list, q + 1, right);
}
return list;
}
int main() {
int n;
cin >> n;
int *list = new int[n];
for (int i = 0; i < n; i++) {
scanf("%d", &list[i]);
}
int* tmp_list = quick_sort(list, 0, n-1);
for (int i = 0; i < n; i++) {
printf("%d ", list[i]);
}
return 0;
}
시간초과라니
'algorithm' 카테고리의 다른 글
2018 KAKAO BLIND RECRUITMENT[1차] 캐시 (0) | 2020.03.26 |
---|---|
2018 KAKAO BLIND RECRUITMENT[1차] 다트 게임 (0) | 2020.03.20 |
2018 KAKAO BLIND RECRUITMENT [1차] 비밀지도 (0) | 2020.03.20 |
[백준/c++][17142] 연구소 3 (0) | 2019.10.17 |
[백준/c++][11052]카드 구매하기 (0) | 2019.10.16 |
댓글