Skip to content

Instantly share code, notes, and snippets.

@anil477
Created November 24, 2017 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/1f4bee305be2f57abc9ef8a5a10182cb to your computer and use it in GitHub Desktop.
Save anil477/1f4bee305be2f57abc9ef8a5a10182cb to your computer and use it in GitHub Desktop.
K largest element in a stream of numbers
// we maintain a min heap of first k element. here we have taken it as 5
// then for the incoming elements we replace the new elements depending on it's value
// if the new elemnet is smaller than the current smallest element it won't be saved in heap
class MinHeap {
public int size;
public int[] mH;
public int position;
public MinHeap(int size)
{
this.size = size;
mH = new int[size + 1];
position = 0;
}
public void createHeap(int[] arrA) {
if (arrA.length > 0) {
for (int i = 0; i < arrA.length; i++) {
insert(arrA[i]);
}
}
}
public void display() {
for (int i = 1; i < mH.length; i++) {
System.out.print(" " + mH[i]);
}
System.out.println("");
}
public void insert(int x) {
// this root element is set as 1 and not 0.
// https://stackoverflow.com/questions/22900388/why-in-a-heap-implemented-by-array-the-index-0-is-left-unused/35460725#35460725
if (position == 0) {
mH[position + 1] = x;
position = 2;
} else {
mH[position++] = x;
bubbleUp();
}
}
public void bubbleUp()
{
int pos = position - 1;
// We get the index of the last element. Then compare it with it's parent which is index/2
// pos > 0 to check that position is still valid
// if the parent is larger than swap it
// next we set the pointer to the parent
// we keep bubbling up until the particular element is at correct position
while (pos > 0 && mH[pos / 2] > mH[pos]) {
int y = mH[pos];
mH[pos] = mH[pos / 2];
mH[pos / 2] = y;
pos = pos / 2; // set the new pointer to point to the parent
}
}
// minimum element is the first element. we will return that.
// next we place the last element to that first position and then start the bubbling down
public int extractMin() {
int min = mH[1];
mH[1] = mH[position - 1];
mH[position - 1] = 0;
position--;
sinkDown(1);
return min;
}
public void sinkDown(int k) {
int smallest = k;
// check if the index passed is greater than the left child. If yes we need to swap it with it
if (2 * k < position && mH[smallest] > mH[2 * k]) {
smallest = 2 * k;
}
// check if the index passed is greater than the right child. If yes we need to swap it with it
if (2 * k + 1 < position && mH[smallest] > mH[2 * k + 1]) {
smallest = 2 * k + 1;
}
// if the current node satisfies the heap property then no need to change anything.
// else swap and call bubble down with the new swapped node
if (smallest != k) {
swap(k, smallest);
sinkDown(smallest);
}
}
public void swap(int a, int b) {
int temp = mH[a];
mH[a] = mH[b];
mH[b] = temp;
}
public void stream()
{
int stream[] = {12, 2600, 10, 1, 32};
for(int i= 0; i < stream.length; i++) {
if(mH[1] < stream[i]) {
extractMin();
insert(stream[i]);
}
}
}
public static void main(String args[]) {
int arrA[] = { 100, 5, 16, 2, 121 };
MinHeap m = new MinHeap(arrA.length);
m.createHeap(arrA);
m.stream();
for (int i = 0; i < arrA.length; i++) {
System.out.print(" " + m.extractMin());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment