본문 바로가기

백준 문제풀기/JAVA

[백준 24060 JAVA 자바] 알고리즘 수업 - 병합 정렬 1

 

코드입니다

import java.util.Scanner;

public class Main {    
        static int[] temp;
        static int count = 0;
        static int K;
        static int result = -1;    
        public static void main(String[] args){        
        Scanner scan = new Scanner(System.in);

        int num = scan.nextInt();
        int[] arr = new int[num];
        K = scan.nextInt();

        temp = new int[num];

        for(int i = 0 ; i < num ; i++){
            arr[i] = scan.nextInt();
        }
        merge_sort(arr, 0, arr.length - 1);
        System.out.print(result);
      }

        static void merge_sort(int A[], int low, int high){

            if(low < high){
                int mid = (low + high) / 2;
                merge_sort(A, low, mid);
                merge_sort(A, mid + 1, high);
                merge(A, low, mid, high);
            }
        }

static void merge(int A[], int low, int mid, int high){
        int i = low;
        int j = mid + 1;
        int t = 0;
        
        while(i <= mid && j <= high){
            if(A[i] <= A[j]){
                temp[t++] = A[i++];
            }else{
                temp[t++] = A[j++];
            }
        }

        while(i <= mid){
            temp[t++] = A[i++];
        }

        while(j <= high){
            temp[t++] = A[j++];
        }

        t = 0;
        i = low;

        while(i <= high){
            count++;
            if(count==K){
                result = temp[t];
                break;
            }
            A[i++] = temp[t++];
        }     
   }
}