와챠의 우당탕탕 개발 기록장

[백준]14-정렬/1-수 정렬하기/2750(C, JAVA) 풀이 본문

코딩 일기장/CodingTest

[백준]14-정렬/1-수 정렬하기/2750(C, JAVA) 풀이

minWachya 2020. 8. 22. 16:48
반응형

문제

N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.

 

입력

첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 숫자가 주어진다.

이 수는 절댓값이 1,000보다 작거나 같은 정수이다. 수는 중복되지 않는다.

 

출력

첫째 줄부터 N개의 줄에 오름차순으로 정렬한 결과를 한 줄에 하나씩 출력한다.

 

풀이 1(C)

#include <stdio.h>
int main(void) {
int N;
scanf("%d", &N);
int num[1000]; //1 <= N <= 1000
for (int i = 0; i < N; i++) {
scanf("%d", &num[i]);
}
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (num[i] > num[j]) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for (int i = 0; i < N; i++)
printf("%d\n", num[i]);
return 0;
}
view raw 2750C-1.c hosted with ❤ by GitHub

 

풀이 2(C) - 버블 정렬

#include <stdio.h>
#include <stdlib.h>
#define swap(type, x, y) do {type t = x; x = y; y = t;} while(0)
/*
<버블 정렬>
요소가 n개인 배열에서 왼쪽으로 n-1회 비교, 교환
ex) 요소가 5개인 5 4 1 2 3 을 오름차순 정렬하기(n=5)
※<>는 비교하여 순서 바꾸기
-첫 번째(5-1번 비교) -두 번째(5-2번 비교)
5 4 1 2<>3 => 5 4 1 2 3 1 5 4 2<>3 => 1 5 4 2 3
5 4 1<>2 3 => 5 4 1 2 3 1 5 4<>2 3 => 1 5 2 4 3
5 4<>1 2 3 => 5 1 4 2 3 1 5<>2 4 3 => 1 2 5 4 3
5<>1 4 2 3 => 1 5 4 2 3
*/
void bubble(int num[], int n) {
//요소가 n개인 배열에서 왼쪽으로 n-1회 비교, 교환
for (int i = 0; i < n-1; i++) {
//왼쪽으로 이동해야하니 j = n-1부터 시작하여 점점 작아짐
for (int j = n-1; j > i; j--) {
//맨 왼쪽 요소(j)와 그 앞의 요소(j-1) 비교하여 정렬
if (num[j-1] > num[j])
swap(int, num[j-1], num[j]);
}
}
}
int main(void) {
int N;
scanf("%d", &N);
int *num; //배열의 첫 번째 요소에 대한 포인터
num = calloc(N, sizeof(int)); //요소가 N개인 int형 배열을 생성
//숫자 입력
for (int i = 0; i < N; i++) {
scanf("%d", &num[i]);
}
//오름차순 정렬(버블 정렬)
bubble(num, N);
//출력
for (int i = 0; i < N; i++)
printf("%d\n", num[i]);
//배열 해제
free(num);
return 0;
}
view raw 2750C-2.c hosted with ❤ by GitHub

 

풀이 1(JAVA)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[] num = new int[N];
//숫자 입력
for (int i = 0; i < N; i++)
num[i] = Integer.parseInt(br.readLine());
br.close();
//숫자 오름차순 정렬
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (num[i] > num[j]) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
//출력
for (int i = 0; i < N; i++)
bw.write(num[i] + "\n");
bw.flush();
bw.close();
}
}
view raw 2750JAVA-1.java hosted with ❤ by GitHub

 

풀이 2(JAVA) - Arrays.sort 함수 이용

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[] num = new int[N];
//숫자 입력
for (int i = 0; i < N; i++)
num[i] = Integer.parseInt(br.readLine());
br.close();
//숫자 오름차순 정렬
Arrays.sort(num);
//출력
for (int i = 0; i < N; i++)
bw.write(num[i] + "\n");
bw.flush();
bw.close();
}
}
view raw 2750JAVA-2.java hosted with ❤ by GitHub

 

풀이 3(JAVA) - 버블 정렬

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
/*
<버블 정렬>
요소가 n개인 배열에서 왼쪽으로 n-1회 비교, 교환
ex) 요소가 5개인 5 4 1 2 3 을 오름차순 정렬하기(n=5)
※<>는 비교하여 순서 바꾸기
-첫 번째(5-1번 비교) -두 번째(5-2번 비교)
5 4 1 2<>3 => 5 4 1 2 3 1 5 4 2<>3 => 1 5 4 2 3
5 4 1<>2 3 => 5 4 1 2 3 1 5 4<>2 3 => 1 5 2 4 3
5 4<>1 2 3 => 5 1 4 2 3 1 5<>2 4 3 => 1 2 5 4 3
5<>1 4 2 3 => 1 5 4 2 3
*/
public class Main {
static int[] num;
static void bubble(int[] num, int n) {
//요소가 n개인 배열에서 왼쪽으로 n-1회 비교, 교환
for (int i = 0; i < n-1; i++) {
//왼쪽으로 이동해야하니 j = n-1부터 시작하여 점점 작아짐
for (int j = n-1; j > i; j--) {
//맨 왼쪽 요소(j)와 그 앞의 요소(j-1) 비교하여 정렬
if (num[j-1] > num[j])
swap(j-1, j);
}
}
}
static void swap(int a, int b) {
int temp = num[a];
num[a] = num[b];
num[b] = temp;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
num = new int[N]; //요소가 N개인 int형 배열을 생성
//숫자 입력
for (int i = 0; i < N; i++)
num[i] = Integer.parseInt(br.readLine());
br.close();
//오름차순 정렬(버블 정렬)
bubble(num, N);
//출력
for (int i = 0; i < N; i++)
bw.write(num[i] + "\n");
bw.flush();
bw.close();
}
}
view raw 2750JAVA-3.java hosted with ❤ by GitHub


버블 정렬이 예시에 있길래 알고리즘 책 뒤져서 따라 해 봤다.

알고리즘... 재밌다!! 신기하다!!

 

알고리즘 책은 C언어밖에 없어서 JAVA는 그냥 비슷하게 해 봤는데

JAVA용 책은 살 필요 없나?

나중에 코로링 덜해지면 서점에서 비교해보고 싶다.

반응형
Comments