๐Ÿ“—
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
  • Heap Sort
  • ํž™์ •๋ ฌ (Heap Sort)
  • ์ •๋ ฌ ๋ฐฉ๋ฒ•

Was this helpful?

  1. algorithm
  2. sort

heapSort

PreviousbubbleSortNextquickSort

Last updated 3 years ago

Was this helpful?

Heap Sort

ํž™์ •๋ ฌ (Heap Sort)

: ํž™ํŠธ๋ฆฌ ๊ตฌ์กฐ (Heap Tree Structure) ๋ฅผ ์ด์šฉํ•˜๋Š” ์ •๋ ฌ ๋ฐฉ๋ฒ• ํž™์€ ์ด์ง„ํŠธ๋ฆฌ (Binart Tree: ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ ๋…ธ๋“œ(node)์— ๋‹ด์€ ๋’ค์— ๋…ธ๋“œ๋ฅผ ๋‘๊ฐœ์”ฉ ์ด์–ด ๋ถ™์ด๋Š” ๊ตฌ์กฐ)

์ •๋ ฌ ๋ฐฉ๋ฒ•

  1. n๊ฐœ์˜ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ์ด๋•Œ ๋ฃจํŠธ ๋…ธ๋“œ๋ถ€ํ„ฐ ๋ถ€๋ชจ๋…ธ๋“œ, ์™ผ์ชฝ ์ž์‹๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ ์ˆœ์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค.

  2. ์ตœ๋Œ€ ํž™์„ ๊ตฌ์„ฑํ•œ๋‹ค. ์ตœ๋Œ€ ํž™์ด๋ž€ ๋ถ€๋ชจ๋…ธ๋“œ๊ฐ€ ์ž์‹๋…ธ๋“œ๋ณด๋‹ค ํฐ ํŠธ๋ฆฌ๋ฅผ ๋งํ•˜๋Š”๋ฐ, ๋‹จ๋ง ๋…ธ๋“œ๋ฅผ ์ž์‹๋…ธ๋“œ๋กœ ๊ฐ€์ง„ ๋ถ€๋ชจ๋…ธ๋“œ๋ถ€ํ„ฐ ๊ตฌ์„ฑํ•˜๋ฉฐ ์•„๋ž˜๋ถ€ํ„ฐ ๋ฃจํŠธ๊นŒ์ง€ ์˜ฌ๋ผ์˜ค๋ฉฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๋งŒ๋“ค์–ด ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

  3. ๊ฐ€์žฅ ํฐ ์ˆ˜(๋ฃจํŠธ์— ์œ„์น˜)๋ฅผ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜์™€ ๊ตํ™˜ํ•œ๋‹ค.

  4. 2์™€ 3์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

class HeapSort {
    // ๋ฐฐ์—ด ์š”์†Œ a[idx1]๊ณผ a[idx2]์˜ ๊ฐ’์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. 
    static void swap(int[] a, int idx1, int idx2) {
        int t = a[idx1];  
        a[idx1] = a[idx2];  
        a[idx2] = t;
    }

    // a[left] ~ a[right]๋ฅผ ํž™์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 
    // a[left] ์ด์™ธ์—๋Š” ๋ชจ๋‘ ํž™์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  a[left]๋ฅผ ์•„๋žซ๋ถ€๋ถ„์˜ ์•Œ๋งž์€ ์œ„์น˜๋กœ ์˜ฏ๊ฒจ ํž™ ์ƒํƒœ๋ฅผ ๋งŒ๋“ ๋‹ค 
    static void downHeap(int[] a, int left, int right) {
        int temp = a[left];                // ๋ฃจํŠธ
        int child;                        // ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ
        int parent;                        // ๋ถ€๋ชจ

        for (parent = left; parent < (right + 1) / 2; parent = child) {
            int cl = parent * 2 + 1;                            // ์™ผ์ชฝ ์ž์‹
            int cr = cl + 1;                                    // ์˜ค๋ฅธ์ชฝ ์ž์‹
            child = (cr <= right && a[cr] > a[cl]) ? cr : cl;    // ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ์ž์‹์— ๋Œ€์ž… 
            if (temp >= a[child])
                break;
            a[parent] = a[child];
        }
        a[parent] = temp;
    }

    // ํž™ ์ •๋ ฌ
    // ์š”์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ n๊ฐœ์ธ ๋ฐฐ์—ด a๋ฅผ ํž™ ์ •๋ ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ด๋‹ค 
    // downHeap๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด a๋ฅผ ํž™์œผ๋กœ ๋งŒ๋“ ๋‹ค 
    // ๋ฃจํŠธ(a[0]) ์— ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋นผ๋‚ด์–ด ๋งˆ์ง€๋ง‰ ์š”์†Œ์™€ ๋ฐ”๊พธ๊ณ  ๋ฐฐ์—ด์˜ ๋‚˜๋จธ๋น„ ๋ถ€๋ถ„์„ ๋‹ค์‹œ ํž™์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ 
    static void heapSort(int[] a, int n) {
        for (int i = (n - 1) / 2; i >= 0; i--)    // a[i] ~ a[n-1]๋ฅผ ํž™์œผ๋กœ ๋งŒ๋“ค๊ธฐ
            downHeap(a, i, n - 1);

        for (int i = n - 1; i > 0; i--) {
            swap(a, 0, i);                // ๊ฐ€์žฅ ํฐ ์š”์†Œ์™€ ์•„์ง ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ถ€๋ถ„์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊ตํ™˜
            downHeap(a, 0, i - 1);        // a[0] ~ a[i-1]์„ ํž™์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
        }
    }

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

        heapSort(x, 10); // ๋ฐฐ์—ด x๋ฅผ ํ€ต ์ •๋ ฌ

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