Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- int pocet = Convert.ToInt32(Console.ReadLine());
- int[] pole = new int[pocet + 1];
- String[] cisla = Console.ReadLine().Split();
- for (int i = 0; i < cisla.Length; i++)
- {
- pole[i + 1] = Convert.ToInt32(cisla[i]);
- }
- pole = HeapSort(pole);
- }
- static int[] HeapSort(int[] pole)
- {
- for (int i = 1; i < pole.Length; i++)
- {
- while (i > 1 && pole[i / 2] > pole[i])
- {
- int x = pole[i / 2];
- pole[i / 2] = pole[i];
- pole[i] = x;
- i = i / 2;
- }
- }
- for (int i = 1; i < pole.Length; i++)
- {
- int x = pole[pole.Length - i];
- pole[pole.Length - i] = pole[1];
- pole[1] = x;
- while (2 * i < (pole.Length - i))
- {
- int j = i;
- int levy = 2*i;
- if (pole[i] < pole[levy]) j = levy;
- if (levy + 1 < pole.Length - i && pole[j] < pole[levy + 1]) j = levy + 1;
- if (i == j) break;
- x = pole[i];
- pole[i] = pole[j];
- pole[j] = x;
- }
- }
- foreach (int i in pole)
- {
- Console.WriteLine(i);
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment