Skip to content

Instantly share code, notes, and snippets.

@kolauren
Created January 30, 2013 06:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kolauren/4671157 to your computer and use it in GitHub Desktop.
Save kolauren/4671157 to your computer and use it in GitHub Desktop.
Simple heap with heapsort
// Heapsort with Java
// Heap properties:
// Left child of h[i] is h[2i + 1] (given 2i + 1 < h.size)
// Right child of h[i] is h[2i + 2] (given 2i + 2 < h.size)
// parent of h[i] is h[(i-1)/2] (given i > 0)
public class Heap {
int[] heap;
int size;
int lastPosition;
public Heap(int[] array) {
heap = array;
this.size = array.length;
lastPosition = array.length - 1;
}
// Return the maximum element in the heap
public int findMax() {
if(size > 0) {
return heap[0];
} else {
return -1;
}
}
// Find the maximum element the heap and delete it O(logn)
public int extractMax() {
int max = heap[0];
delete(0);
return max;
}
// Insert a new element into the heap O(logn)
public void insert(int x) {
heap[lastPosition] = x;
siftUp(heap, lastPosition);
lastPosition++;
}
// Delete element at location i O(logn)
public void delete(int i) {
heap[i] = heap[lastPosition];
lastPosition--;
size--;
// One of these will do nothing.
siftUp(heap, i);
siftDown(heap, i);
}
// move item at location i up to its correct position O(logn)
public void siftUp(int[] h, int i) {
int parent = (int)Math.floor((i-1)/2);
if(i > 0 && h[parent] < h[i]) {
int temp = h[i];
h[i] = h[parent];
h[parent] = temp;
siftUp(h, parent);
}
}
// move item at location i down to its correct position O(logn)
public void siftDown(int[] h, int i) {
int leftChild = 2*i + 1;
int rightChild = 2*i + 2;
int largerChild;
if(rightChild < size && h[rightChild] > h[leftChild]) {
largerChild = rightChild;
} else {
largerChild = leftChild;
}
if(largerChild < size && h[largerChild] > h[i]) {
int temp = h[i];
h[i] = h[largerChild];
h[largerChild] = temp;
siftDown(h, largerChild);
}
}
// Create a heap from unsorted array (n is size of array)
// You're only running siftDown on half of the array,
// so it actually runs in O(n) rather than regular insertion
// which would run in O(nlogn)
public void heapify(int[] h) {
for(int i = (int)Math.floor((size-1)/2); i >= 0; i--) {
siftDown(h,i);
}
}
// returns a sorted array, using in-place heapsort
// O(nlogn)
public int[] heapSort() {
heapify(heap);
for(int i = size - 1; i >= 1; i--) {
int max = extractMax();
heap[i] = max;
}
return heap;
}
public void print() {
System.out.println("Printing heap //////");
for(int i = 0; i < heap.length; i++){
System.out.println(heap[i]);
}
}
public static void main(String[] args) {
int[] integers = {36, 31, 20, 40, 23, 18, 15, 79, 27, 83};
Heap heapy = new Heap(integers);
int[] result = heapy.heapSort();
System.out.println("printing result /////");
for(int i = 0; i < result.length; i++){
System.out.println(result[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment