๐Ÿ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • Merge Sort
  • ๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)
  • ์ •๋ ฌ ๋ฐฉ๋ฒ•

Was this helpful?

  1. algorithm
  2. sort

mergeSort

PrevioussortNextinsertionSort

Last updated 3 years ago

Was this helpful?

Merge Sort

๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)

: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์›์†Œ๊ฐ€ ํ•˜๋‚˜ ๋ฐ–์— ๋‚จ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋‘˜๋กœ ์ชผ๊ฐ  ํ›„์— ๋‹ค์‹œ ํฌ๊ธฐ ์ˆœ์œผ๋กœ ์žฌ๋ฐฐ์—ด ํ•˜๋ฉด์„œ ์›๋ž˜ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด๋กœ ํ•ฉ์นœ๋‹ค

  • ๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ๋ถ„ํ•  ์ •๋ณต ๊ธฐ๋ฒ•๊ณผ ์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(NlogN)์ด๋‹ค

  • ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(N)

  • ๋‹ค๋ฅธ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋‹ฌ๋ฆฌ ์ธ์ ‘ํ•œ ๊ฐ’๋“ค ๊ฐ„์— ์ƒํ˜ธ ์ž๋ฆฌ ๊ต๋Œ€(swap)์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค

์ •๋ ฌ ๋ฐฉ๋ฒ•

๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ 1 ์ดํ•˜์ด๋ฉด ์ด๋ฏธ ์ •๋ ฌ๋œ ๊ฒƒ์œผ๋กœ ๋ณธ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” 1. ๋ถ„ํ• (divide) : ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ ˆ๋ฐ˜์œผ๋กœ ์ž˜๋ผ ๋น„์Šทํ•œ ํฌ๊ธฐ์˜ ๋‘ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜๋ˆˆ๋‹ค. 2. ์ •๋ณต(conquer) : ๊ฐ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ•ฉ๋ณ‘ ์ •๋ ฌ์„ ์ด์šฉํ•ด ์ •๋ ฌํ•œ๋‹ค. 3. ๊ฒฐํ•ฉ(combine) : ๋‘ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋กœ ํ•ฉ๋ณ‘ํ•œ๋‹ค. ์ด๋•Œ ์ •๋ ฌ ๊ฒฐ๊ณผ๊ฐ€ ์ž„์‹œ๋ฐฐ์—ด์— ์ €์žฅ๋œ๋‹ค. 4. ๋ณต์‚ฌ(copy) : ์ž„์‹œ ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ์›๋ž˜ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•œ๋‹ค.

public class MergeSort {
    private static void sort(int[] arr, int low, int high) {
        if (high - low < 2) {
            return;
        }

        int mid = (low + high)/2;
        sort(arr, 0, mid);
        sort(arr, mid, high);
        merge(arr, low, mid, high);
    }

    private static void merge(int[] arr, int low, int mid, int high) {
        int[] temp = new int[high - low];
        int t = 0;
        int l = low;
        int m = mid;

        while(l < mid && m < high) {
            if (arr[l] < arr[m]) {
                temp[t++] = arr[l++];
            } else {
                temp[t++] = arr[m++];
            }
        }

        while (l < mid) {
            temp[t++] = arr[l++];
        }

        while (m < high) {
            temp[t++] = arr[m++];
        }

        for (int i = low; i < high; i++) {
            arr[i] = temp[i - low];
        }
    }

    public static void main(String[] args) {
        int[] x = { 1, 10, 5, 8, 7, 6, 4, 3, 2, 9 };

        sort(x, 0, x.length);

        for (int i = 0; i <  x.length; i++)
            System.out.println("x[" + i + "]๏ผ" + x[i]);

    }
}

Reference : Wikipedia
MergeSort