static void Main() { Console.ReadLine(); int[] inputs = Console.ReadLine().Split(' ') .Select(val => int.Parse(val)) .ToArray(); Heap maxHeap = new Heap(inputs); maxHeap.BuildMaxHeap(); maxHeap.Display(); }
public void HeapSort(int[] arrayToSort) { var heap = new Heap(arrayToSort); heap.BuildMaxHeap(); while (heap.HeapSize > 1) { heap.SwapHeapValues(1, heap.HeapSize--); heap.MaxHeapify(1); } }
public int LastStoneWeightCalc(int[] stones) { List <int> stonesList = stones.ToList(); int cntZeroes = 0; int maxIterations = stones.Length - 1; heap.BuildMaxHeap(stonesList); while (cntZeroes < maxIterations) { int largest = stonesList[0]; int secondLargestIndex = heap.Left(stonesList, 0); if (stonesList.Count > 2) { secondLargestIndex = stonesList[secondLargestIndex] > stonesList[heap.Right(stonesList, 0)] ? secondLargestIndex : heap.Right(stonesList, 0); } if (largest == stonesList[secondLargestIndex]) { stonesList[0] = 0; stonesList[secondLargestIndex] = 0; cntZeroes += 2; } else { stonesList[0] = largest - stonesList[secondLargestIndex]; stonesList[secondLargestIndex] = 0; cntZeroes += 1; } heap.BuildMaxHeap(stonesList); } if (stonesList.Count == 0) { return(0); } return(stonesList[0]); }
private void Push(int n) { if (_arrCnt.ContainsKey(n)) { _arrCnt[n]++; } else { _arrCnt.Add(n, 1); _arrMaxHeap.Add(n); _arrMinHeap.Add(n); _heap.BuildMaxHeap(_arrMaxHeap); _heap.BuildMinHeap(_arrMinHeap); } }
public PriorityQueue(IEnumerable <T> data, PriorityOrder order) { _data = data as IList <T>; if (_data == null) { _data = new List <T>(data); } _order = order; _heapSize = _data.Count; if (_order == PriorityOrder.Max) { Heap.BuildMaxHeap(_data); } else { Heap.BuildMinHeap(_data); } }