Guest User

Untitled

a guest
Apr 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Heap
  9.     {
  10.        
  11.         public int[] Input;
  12.         public int n;
  13.  
  14.         public Heap(int[] Input)
  15.         {
  16.             n = Input.Length;
  17.             int[] temp = new int[n+1];
  18.             for (int i = 0; i < Input.Length; ++i)
  19.             {
  20.                 temp[i] = Input[i];
  21.             }
  22.             this.Input = temp;
  23.         }
  24.  
  25.         public void HeapCorrectOrder()
  26.         {            
  27.             int[] temp = new int[n];
  28.             for (int i = 0; i < n; i++)
  29.             {
  30.                 temp[i] = Input[n - i-1];
  31.             }
  32.             Input = temp;
  33.  
  34.         }
  35.  
  36.         public void HeapConstruct()
  37.         {
  38.             int i;
  39.             int temp;
  40.             for (i = n / 2 - 1; i >= 0; i--)
  41.             {
  42.                 DownShift(i, n);
  43.             }
  44.             for (i = n - 1; i >= 1; i--)
  45.             {
  46.                 temp = Input[0];
  47.                 Input[0] = Input[i];
  48.                 Input[i] = temp;
  49.                 DownShift(0, i - 1);
  50.             }
  51.             HeapCorrectOrder();
  52.         }
  53.         public void DownShift(int Value, int start)
  54.         {
  55.             bool completion = false;
  56.             int ChildBig;
  57.             int temp;
  58.  
  59.             while ((Value * 2 <= start) && (completion != true))
  60.             {
  61.                 if ((Value * 2 == start))
  62.                 {
  63.                     ChildBig = Value * 2;
  64.                 }
  65.                 else if (Input[Value * 2] > Input[Value * 2 + 1])
  66.                 {
  67.                     ChildBig = Value * 2;
  68.                 }
  69.                 else
  70.                 {
  71.                     ChildBig = Value * 2 + 1;
  72.                 }
  73.                 if (Input[Value] < Input[ChildBig])
  74.                 {
  75.                     temp = Input[Value];
  76.                     Input[Value] = Input[ChildBig];
  77.                     Input[ChildBig] = temp;
  78.                     Value = ChildBig;
  79.                 }
  80.                 else
  81.                 {
  82.                     completion = true;
  83.                 }
  84.             }
  85.         }
  86.  
  87.  
  88.  
  89.     }
  90. }
  91.  
  92.  
  93.  
  94. using System;
  95. using System.Collections.Generic;
  96. using System.Linq;
  97. using System.Text;
  98.  
  99. namespace ConsoleApplication1
  100. {
  101.     class Program
  102.     {
  103.  
  104.         static void Main(string[] args)
  105.         {
  106.             int[] Input = new int[10] {13, 34, 19, 24, 2, 1, 26, 87, 20, 2391};
  107.             Heap Heap = new Heap(Input);
  108.             Heap.HeapConstruct();
  109.             for (int i = 0; i < Heap.n; ++i)
  110.             {
  111.                 Console.Write(Heap.Input[i]);
  112.                 if (i < Heap.n-1)
  113.                     Console.Write(", ");
  114.             }
  115.         }
  116.  
  117.     }
  118. }
Add Comment
Please, Sign In to add comment