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 | 31 |
Tags
- 해시
- BFS
- 코테준비
- BruteForceSearch
- 고득점Kit
- C++
- 알고리즘
- Python
- 이분그래프판별
- 동적계획법
- 문제풀이
- 프로그래머스
- LV.3
- 코딩테스트준비
- Lv.2
- 쿠쉬쿠쉬
- 코딩테스트
- Lv.1
- Algorithm
- 단지번호붙이기
- OS
- 그래프
- 파이썬
- 면접질문
- Java
- 2색칠하기
- C
- 운영체제
- hash
Archives
- Today
- Total
쿠쿠의기록
9. 퀵 정렬 본문
문제
N개의 자연수가 주어질 때, 퀵정렬을 이용하여 이를 정렬하는 프로그램을 작성하시오.
입력
첫 번째 줄에 N이 주어진다. ( 1 ≤ N ≤ 100,000 ) 두 번째 줄에 N개의 자연수가 주어진다.
출력
퀵정렬을 이용하여 숫자를 오름차순으로 정렬한 결과를 출력한다.
예제 입력
10 5 9 2 8 3 7 4 6 1 10
예제 출력
1 2 3 4 5 6 7 8 9 10
예제 입력
5 2 3 1 2 1
예제 출력
1 1 2 2 3
문제풀이
#include <stdio.h>
int getLeft(int arr[],int start, int end, int pivot, int result[]){
//arr배열 start부터 end까지 pivot보다 작거나 같은값 result저장
int inx=0;
for(int i=start;i<=end;i++){
if(arr[i]<=pivot){
result[inx++] = arr[i];
}
}
return inx;
}
int getRight(int arr[],int start, int end, int pivot, int result[]){
//arr배열 start부터 end까지 pivot보다 큰수 rresult저장
int inx = 0;
for(int i=start;i<=end;i++){
if(arr[i]>pivot){
result[inx++] = arr[i];
}
}
return inx;
}
void quickSort(int arr[],int start,int end){
//arr배열 start부터 end까지 퀵 정렬
//기저조건
if(start>=end){
return;
}
int left[100010],right[100010];
int pivot = arr[start];
int left_cnt = getLeft(arr,start+1,end,pivot,left);
int right_cnt = getRight(arr,start+1,end,pivot,right);
for(int i=0;i<left_cnt;i++){
arr[start+i] = left[i];
}
arr[start+left_cnt] = pivot;
for(int i=0;i<right_cnt;i++){
arr[start+left_cnt+1+i] = right[i];
}
quickSort(arr,start,start+left_cnt-1);
quickSort(arr,start+left_cnt+1,end);
}
int main(){
int n;
scanf("%d",&n);
int arr[100010];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
quickSort(arr,0,n-1);
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
'알고리즘 > L9~10 고급정렬' 카테고리의 다른 글
10. 나무 자르기 (0) | 2020.09.08 |
---|---|
10. 두 용액 (0) | 2020.09.08 |
10. 숫자 개수 세기 (0) | 2020.09.08 |
10. 이진탐색 (0) | 2020.09.08 |
9. 합병정렬 (0) | 2020.09.08 |