quickly find the median of a sequence of numbers

来源:互联网 发布:java开源仓库管理系统 编辑:程序博客网 时间:2024/06/05 22:44

Question:

Assume the user can enter a large sequence of integer numbers, please return the median of the entered numbers.

Idea: 

Create an min-heap and max-heap, and save the entered number to the heaps and also make sure the heap size difference is less than 1 and the maximum value of max-heap is smaller than or equal to the minimum value of the min-heap.

public class MaxHeap {ArrayList<Integer> list = null;public MaxHeap() {list = new ArrayList<Integer>();}public void insertTop(int value) {list.add(value);buildMaxHeap();}//get the element with maximum valuepublic int getTop() {if (list.size() == 0) return Integer.MIN_VALUE;return list.get(0);}// remove the element with maximum valuepublic void removeTop() {if (list.size() == 0) return;list.remove(0);buildMaxHeap();}//return the number of elements in the arraypublic int size() {return list.size();}public void buildMaxHeap() {int heapSize = list.size();    for (int i = heapSize / 2 - 1; i >= 0; i--) {    maxHeapify(list, i);    }}public void maxHeapify(ArrayList<Integer> heap, int index) {        int position = index;        int left = 2 * index + 1;        int right = 2 * index + 2;                 if(left < heap.size() && heap.get(left) > heap.get(position)) {            position = left;        }                if(right < heap.size() && heap.get(right) > heap.get(position)) {            position = right;        }                if (position != index) {            Integer temp = heap.get(position);            heap.set(position, heap.get(index));            heap.set(index, temp);            maxHeapify(heap, position);        }    }}

import java.util.ArrayList;public class MinHeap {ArrayList<Integer> list = null;public MinHeap() {list = new ArrayList<Integer>();}//return the number of elements in the arraypublic int size() {return list.size();}//get the element with minimum valuepublic int getTop() {if (list.size() == 0) return Integer.MAX_VALUE;return list.get(0);}//insert the value into heappublic void insertTop(int value) {list.add(value);buildMinHeap();}// remove the first elementpublic void removeTop() {if (list.size() == 0) return;list.remove(0);buildMinHeap();}public void buildMinHeap() {int heapSize = list.size();    for (int i = heapSize / 2 - 1; i >= 0; i--) {    minHeapify(list, i);    }}public void minHeapify(ArrayList<Integer> heap, int index) {        int position = index;        int left = 2 * index + 1;        int right = 2 * index + 2;                 if(left < heap.size() && heap.get(left) < heap.get(position)) {            position = left;        }                if(right < heap.size() && heap.get(right) < heap.get(position)) {            position = right;        }                if (position != index) {            Integer temp = heap.get(position);            heap.set(position, heap.get(index));            heap.set(index, temp);            minHeapify(heap, position);        }    }}

public class Receiver {MaxHeap maxHeap = null;MinHeap minHeap = null;public Receiver() {maxHeap = new MaxHeap();minHeap = new MinHeap();}// insert the data into the DataSavepublic void insert(int value) {if (maxHeap.size() == 0) {maxHeap.insertTop(value);return;}if (minHeap.size() == 0) {minHeap.insertTop(value);return;}if (maxHeap.size() == minHeap.size()) {if (value > maxHeap.getTop()) {minHeap.insertTop(value);} else {maxHeap.insertTop(value);}} else if (maxHeap.size() > minHeap.size()) {if (value >= maxHeap.getTop()) {minHeap.insertTop(value);} else {minHeap.insertTop(maxHeap.getTop());maxHeap.removeTop();maxHeap.insertTop(value);}} else {if (value >= minHeap.getTop()) {maxHeap.insertTop(minHeap.getTop());minHeap.removeTop();minHeap.insertTop(value);} else {maxHeap.insertTop(value);}}} //get the medianpublic float median() {if (maxHeap.size() == 0 && minHeap.size() == 0) return Float.MIN_VALUE;    if (maxHeap.size() == minHeap.size()) {        return (maxHeap.getTop() + minHeap.getTop()) / 2.0f;    } else if (maxHeap.size() > minHeap.size()) {        return maxHeap.getTop();        } else {    return minHeap.getTop();    }}}

blog.csdn.net/beiyetengqing