static void Main(string[] args) { int[] array = new int[] { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7, 4, 3, 19, 8, 33, 45 }; int[] testArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var biggestPart = new MinHeap(); var smallesPart = new MaxHeap(); int oddMedian = -1; for (int i = 0; i < testArray.Length; i++) { var median = testArray[i]; median = SwapWithMin(median, biggestPart); median = SwapWithMax(median, smallesPart); if (i % 2 == 0) { Console.WriteLine(median); oddMedian = median; } else { if (median < oddMedian) { smallesPart.Insert(median); biggestPart.Insert(oddMedian); } else { smallesPart.Insert(oddMedian); biggestPart.Insert(median); } Console.WriteLine(((double)median + oddMedian) / 2); } /*Console.WriteLine(); * Console.WriteLine(); * Console.WriteLine(); * Console.WriteLine("++++++++++++++++++++++++++++++++++++++++"); * Console.WriteLine("Smallest part"); * smallesPart.Print(); * * Console.WriteLine("Biggest part"); * biggestPart.Print(); * * Console.WriteLine("++++++++++++++++++++++++++++++++++++++++");*/ } }
private static int SwapWithMax(int number, MaxHeap heap) { if (heap.Size > 0 && number < heap.Top) { var result = heap.ExtractTop(); heap.Insert(number); return(result); } else { return(number); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Heap x = new MaxHeap(); x.Add(13); x.Add(20); x.Add(9); x.Add(7); x.Add(8451); x.Add(1010); x.Add(4900); Console.WriteLine(String.Concat(Array.ConvertAll(x.Items.ToArray(), item => $"{item} "))); while (x.Length() > 0) { Console.WriteLine(x.Poll()); } }