static void Main(string[] args) { Random random = new Random(); int m = 10; MaxHeap <int> heap = new MaxHeap <int>(m); while (m > 0) { heap.insert(random.Next(1, 100)); m--; } heap.printHeap(); Console.WriteLine(); int maxHeapItem = heap.delMax(); Console.WriteLine("Max:" + maxHeapItem); heap.printHeap(); // Console.WriteLine(heap.size()); Console.WriteLine(); Console.WriteLine("----Index Max Heap----"); int n = 5; IndexMaxHeap <int> indexHeap = new IndexMaxHeap <int>(n); for (int k = 0; k < n; k++) { indexHeap.insert(k, random.Next(1, 10)); } indexHeap.printIndexHeap(); Console.WriteLine("---------------------"); int max = indexHeap.delMax(); Console.WriteLine("Delete Max Item:" + max); indexHeap.printIndexHeap(); Console.WriteLine("---------------------"); int maxIndex = indexHeap.delMaxIndex(); Console.WriteLine("Delete Max Item Index:" + maxIndex); indexHeap.printIndexHeap(); //Console.WriteLine("---------------------"); //indexHeap.change(2, 8); //indexHeap.printIndexHeap(); }
public static void Heapsort(int[] array, int n) { for (int i = n / 2 - 1; i >= 0; i--) { MaxHeap.MaxHeapify(ref array, n, i); } for (int i = n - 1; i >= 0; i--) { int temp = array[i]; array[i] = array[0]; array[0] = temp; MaxHeap.MaxHeapify(ref array, i, 0); } }
static void Main(string[] args) { var heap = new MaxHeap(); heap.list = new System.Collections.Generic.List <int> { 66, 14, 98, 45, 17, 9, 28, 37, 59, 46, 52, 55, 59 }; heap.BuildHeap(); heap.Insert(72); var retVal = heap.GetSortedList(); retVal.ForEach(Console.WriteLine); }
static void Main(string[] args) { MaxHeap heap = new MaxHeap(); MaxHeap maxHeap = new MaxHeap(); var a = new int[] { 45, 35, 15, 30, 10, 12, 6, 5, 20, 50 }; DisplayHeap(a); Console.WriteLine("Inplace Insert"); heap.InsertInplace(a, 9); //insert 50 at index 9 DisplayHeap(a); Console.WriteLine("Create Heap"); int[] b = { 10, 20, 30, 25, 5, 40, 35 }; /* * List<int> arrList = new List<int>(); * maxHeap.CreateHeap(arrList, b, b.Length); * DisplayHeap(arrList);*/ Console.WriteLine("Inplace Insert"); maxHeap.CreateHeap(b, b.Length); DisplayHeap(b); //DELETE FROM HEAP Console.WriteLine("Delete:\n"); MaxHeap mh = new MaxHeap(); int[] b1 = { 10, 20, 30, 25, 5, 40, 35 }; //output: 40,25,35,10,5,20,30 for (int i = 0; i < b1.Length; i++) //heap size=7 here { mh.InsertInplace(b1, i); } DisplayHeap(b1); for (int i = b1.Length; i > 0; i--) { Console.Write("{0}\t", maxHeap.Delete(b1, i)); } Console.WriteLine("\n"); DisplayHeap(b1); HeapifyCreate(); Console.ReadKey(); }
static void HeapifyCreate() { Console.WriteLine("\n\nHeapify: \n"); MaxHeap heapify = new MaxHeap(); int[] A = { 5, 10, 30, 20, 35, 40, 15 }; Console.WriteLine("Size of A = {0}", A.Length); heapify.Heapify(A, A.Length); DisplayHeap(A); int[] B = { 5, 10, 30, 20 }; Console.WriteLine("Size of B = {0}", B.Length); heapify.Heapify(B, B.Length); DisplayHeap(B); }