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.
blog.csdn.net/beiyetengqing
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
相关文章推荐
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Find the smallest window of a certain sequence
- 求三个数的中数 find median of three numbers
- Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
- [Google]Find numbers of nodes in a BST in the range [low,high]
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
- Find the median of 2 sort ed array.
- Find the number of subsets such that the sum of numbers in the subset is a prime number
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- Find the maximum of the three numbers(Take this opportunity to review the sorting algorithm)
- Find the median of two sorted arrays(找到两个排好序的数组的中位数)
- Spring 配置文件报错: cvc-elt.1: Cannot find the declaration of element ""beans""
- 题目1442:A sequence of numbers 数列题
- windows下python 安装igraph库报错 Cannot find the C core of igraph on this system using pkg-config.的解决
- HDUOJ---The number of divisors(约数) about Humble Numbers
- 1069.The Black Hole of Numbers
- projecteuler网站第四题解法:Find the largest palindrome made from the product of two 3-digit numbers
- Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans'
- 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决办法。