您的位置:首页 > 产品设计 > UI/UE

quickly find the median of a sequence of numbers

2013-11-02 06:37 323 查看
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 value
public int getTop() {
if (list.size() == 0) return Integer.MIN_VALUE;
return list.get(0);
}
// remove the element with maximum value
public void removeTop() {
if (list.size() == 0) return;
list.remove(0);
buildMaxHeap();
}

//return the number of elements in the array
public 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 array
public int size() {
return list.size();
}

//get the element with minimum value
public int getTop() {
if (list.size() == 0) return Integer.MAX_VALUE;
return list.get(0);
}

//insert the value into heap
public void insertTop(int value) {
list.add(value);
buildMinHeap();
}
// remove the first element
public 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 DataSave
public 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 median
public 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐