static IEnumerable <int> MedianMaintenance(IEnumerable <int> xs) { MaxPQ <int> lowValues = PQ <int> .CreateMaxPQ(); MinPQ <int> highValues = PQ <int> .CreateMinPQ(); int y = 0; foreach (var x in xs) //lg(N!) + 4/3 lg (N!) == 7/3 lg (N!) { if (lowValues.Size > 0) //lg(N!) { if (x > lowValues.Top) { highValues.Push(x); //lg N } else { lowValues.Push(x); //lg N } } else { lowValues.Push(x); //lg N } //TODO: Not a simple analysis, depends on order of data in xs //Draft: Suppose 1/3 per branch and 2 lg N Push and Extract, the expected value would be 4/3 lg (N!) if (lowValues.Size > highValues.Size + 1) { highValues.Push(ExtractMax(lowValues)); //2 lg N (Push + Extract) } else if (highValues.Size > lowValues.Size) { lowValues.Push(ExtractMin(highValues)); //2 lg N (Push + Extract) } //else { } //0 y = lowValues.Top; yield return(y); //DEBUG //Console.WriteLine("Median of {0,4} = {1,4}", lowValues.Size + highValues.Size, y); } }