예제 #1
0
        public static float[] GetRunningMedians(int[] a)
        {
            float[]          medians = new float[a.Length];
            BinaryHeap <int> maxHeap = new BinaryHeap <int>(new MaxSort());
            BinaryHeap <int> minHeap = new BinaryHeap <int>();

            for (int i = 0; i < a.Length; ++i)
            {
                int val = a [i];
                if (minHeap.Count == 0)
                {
                    minHeap.Insert(val);
                }
                else
                {
                    if (val > minHeap.Peek())
                    {
                        minHeap.Insert(val);
                    }
                    else
                    {
                        maxHeap.Insert(val);
                    }
                }

                if (minHeap.Count > maxHeap.Count + 1)
                {
                    maxHeap.Insert(minHeap.RemoveRoot());
                }
                else if (maxHeap.Count > minHeap.Count)
                {
                    minHeap.Insert(maxHeap.RemoveRoot());
                }

                medians [i] = (minHeap.Count > maxHeap.Count ? (float)minHeap.Peek() : ((float)(maxHeap.Peek() + minHeap.Peek()) / 2f));
            }

            return(medians);
        }