[Algorithm/C] BOJ.2751 수 정렬하기 2
문제
N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.
입력
첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 숫자가 주어진다. 이 수는 절대값이 1,000,000보다 작거나 같은 정수이다. 수는 중복되지 않는다.
출력
첫째 줄부터 N개의 줄에 오름차순으로 정렬한 결과를 한 줄에 하나씩 출력한다.
예제 입력 1 복사
5
5
4
3
2
1
예제 출력 1 복사
1
2
3
4
5
출처: <https://www.acmicpc.net/problem/2751>
/*
Problem : 수정렬하기
Writer : JaeIn Mun
Final revision : 2018.05.21
Reference :
https://www.acmicpc.net/problem/2751
http://jaeonysos.tistory.com
*/
#include <stdio.h>
#include <stdlib.h>
int comp(const void *a, const void *b) {
const int *m, *n;
m = (int *)a;
n = (int *)b;
if (*m < *n) {
return -1;
}
else if (*m == *n) {
return 0;
}
else {
return 1;
}
}
int main(void)
{
int n;
int *arr = NULL;
scanf("%d", &n);
arr = (int*)malloc(sizeof(int)*(n));
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), comp);
for (int i = 0; i < n; i++) {
printf("%d\n", arr[i]);
}
free(arr);
return 0;//Your program should return 0 on normal termination.
}
*Quick sort