Algorithm

Heap Sort

orioncsy 2022. 10. 11. 10:19
import java.util.*;

public class Solution { 
	public int[] heapSort(int[] arr) {
    //우선순위 큐를 선언하여 heap을 구현 가능
    PriorityQueue<Integer> heap = new PriorityQueue<>();
    //java에서는 기본이 minheap
    //maxheap :  PriorityQueue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());
    for(int el : arr)
      heap.add(el);
    for(int i=0; i<arr.length; i++)
      arr[i]=heap.poll();
    return arr;
	}
}