public static T[] Sort(T[] input) { var heap = new MaxHeap <T>(input.Length, input); for (int i = input.Length - 1; i >= 1; i--) { var tmp = heap.array[0]; heap.array[0] = heap.array[i]; heap.heapSize--; heap.Heapify(0); } return(heap.array); }
// usage public static int minSum(List <int> num, int k) { MaxHeap heap = new MaxHeap(); // fill the heap. heap.Heapify(num, num.Count); HeapNode data; for (int i = 0; i < k; i++) { data = heap.GetMax(); int newValue = (int)Math.Ceiling(num[data.Index] / 2.0); num[data.Index] = newValue; heap.ChangeMax(newValue); } return(num.Sum()); }