Hard deck/reindexing d3

020 : merge sort // 수 정렬하기

서버관리자 페페 2023. 6. 27. 10:55

 

 

 

 

 

 

flush와 close

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

recursive bottom(최소 피스)

 

구간 나눠서 하청 주기

 

작성할 connector / pointer 확인

(TP connector와 pointer / cont connector와 pointer x 2 사용)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

LEFTOVER

 

 

 

 

 

 

 

import java.io.*;

public class Main {
    static int[] TP;
    static int[] cont;
    public static void main(String[] args) throws IOException {

        // ISC
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int V = Integer.parseInt(br.readLine());

        // D2F
        TP = new int[V+1];
        cont = new int[V+1];
        for (int i = 1; i <= V; i++) {
            TP[i] = Integer.parseInt(br.readLine());
        }

        // OP
        merge(1, V);

        // OEC
        for (int i = 1; i <= V; i++) {
            bw.write(TP[i] + "\n");
        }
        bw.flush();
        bw.close();
    }

    static void merge(int s, int e) {

        if (e - s < 1)
            return;

        int m = s + (e-s)/2;
        merge(s, m);
        merge(m+1, e);

        for (int i = s; i <= e; i++) {
            cont[i] = TP[i];
        }
        int k = s;
        int p1 = s;
        int p2 = m+1;

        while (p1 <= m && p2 <= e) {
            if (cont[p1] < cont[p2]) {
                TP[k] = cont[p1];
                k++;
                p1++;
            } else {
                TP[k] = cont[p2];
                k++;
                p2++;
            }
        }
        while (p1 <= m) {
            TP[k] = cont[p1];
            k++;
            p1++;
        }
        while (p2 <= e) {
            TP[k] = cont[p2];
            k++;
            p2++;
        }
    }
}

 

'Hard deck > reindexing d3' 카테고리의 다른 글

077 : 이항계수 구하기 2  (0) 2023.06.28
019 : Quick sort / K번째 수 구하기  (0) 2023.06.28
018 : ATM 인출 시간 / insertion sort  (0) 2023.06.27
017 : selection sort / 내림차순  (0) 2023.06.27
021 : Bubble sort program2  (0) 2023.06.27