Guest User

Untitled

a guest
Jan 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             int pocet = Convert.ToInt32(Console.ReadLine());
  6.             int[] pole = new int[pocet + 1];
  7.  
  8.             String[] cisla = Console.ReadLine().Split();
  9.             for (int i = 0; i < cisla.Length; i++)
  10.             {
  11.                 pole[i + 1] = Convert.ToInt32(cisla[i]);
  12.             }
  13.  
  14.             pole = HeapSort(pole);
  15.  
  16.         }
  17.  
  18.         static int[] HeapSort(int[] pole)
  19.         {
  20.  
  21.             for (int i = 1; i < pole.Length; i++)
  22.             {
  23.                 while (i > 1 && pole[i / 2] > pole[i])
  24.                 {
  25.                     int x = pole[i / 2];
  26.                     pole[i / 2] = pole[i];
  27.                     pole[i] = x;
  28.                     i = i / 2;
  29.                 }
  30.             }
  31.  
  32.             for (int i = 1; i < pole.Length; i++)
  33.             {
  34.                 int x = pole[pole.Length - i];
  35.                 pole[pole.Length - i] = pole[1];
  36.                 pole[1] = x;
  37.  
  38.                 while (2 * i < (pole.Length - i))
  39.                 {
  40.                     int j = i;
  41.                     int levy = 2*i;
  42.  
  43.                     if (pole[i] < pole[levy]) j = levy;
  44.                     if (levy + 1 < pole.Length - i && pole[j] < pole[levy + 1]) j = levy + 1;
  45.                     if (i == j) break;
  46.  
  47.                     x = pole[i];
  48.                     pole[i] = pole[j];
  49.                     pole[j] = x;
  50.                 }
  51.             }
  52.  
  53.             foreach (int i in pole)
  54.             {
  55.                 Console.WriteLine(i);
  56.             }
  57.  
  58.             return null;
  59.  
  60.  
  61.         }
  62.     }
Add Comment
Please, Sign In to add comment