Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Heap
- {
- public int[] Input;
- public int n;
- public Heap(int[] Input)
- {
- n = Input.Length;
- int[] temp = new int[n+1];
- for (int i = 0; i < Input.Length; ++i)
- {
- temp[i] = Input[i];
- }
- this.Input = temp;
- }
- public void HeapCorrectOrder()
- {
- int[] temp = new int[n];
- for (int i = 0; i < n; i++)
- {
- temp[i] = Input[n - i-1];
- }
- Input = temp;
- }
- public void HeapConstruct()
- {
- int i;
- int temp;
- for (i = n / 2 - 1; i >= 0; i--)
- {
- DownShift(i, n);
- }
- for (i = n - 1; i >= 1; i--)
- {
- temp = Input[0];
- Input[0] = Input[i];
- Input[i] = temp;
- DownShift(0, i - 1);
- }
- HeapCorrectOrder();
- }
- public void DownShift(int Value, int start)
- {
- bool completion = false;
- int ChildBig;
- int temp;
- while ((Value * 2 <= start) && (completion != true))
- {
- if ((Value * 2 == start))
- {
- ChildBig = Value * 2;
- }
- else if (Input[Value * 2] > Input[Value * 2 + 1])
- {
- ChildBig = Value * 2;
- }
- else
- {
- ChildBig = Value * 2 + 1;
- }
- if (Input[Value] < Input[ChildBig])
- {
- temp = Input[Value];
- Input[Value] = Input[ChildBig];
- Input[ChildBig] = temp;
- Value = ChildBig;
- }
- else
- {
- completion = true;
- }
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] Input = new int[10] {13, 34, 19, 24, 2, 1, 26, 87, 20, 2391};
- Heap Heap = new Heap(Input);
- Heap.HeapConstruct();
- for (int i = 0; i < Heap.n; ++i)
- {
- Console.Write(Heap.Input[i]);
- if (i < Heap.n-1)
- Console.Write(", ");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment