-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap-sort.cs
61 lines (58 loc) · 1.63 KB
/
heap-sort.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
namespace Base
{
class Program
{
public static void Heapify(ref int[] array, int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if(l < n && array[i] < array[l])
{
largest = l;
}
if(r < n && array[largest] < array[r])
{
largest = r;
}
if(largest != i)
{
array[i] = array[i] + array[largest];
array[largest] = array[i] - array[largest];
array[i] = array[i] - array[largest];
Heapify(ref array, n, largest);
}
}
public static void Sort(ref int[] array)
{
for(int i = array.Length / 2; i > -1; i --)
{
Heapify(ref array, array.Length, i);
}
for(int j = array.Length - 1; j > 0; j --)
{
array[j] = array[j] + array[0];
array[0] = array[j] - array[0];
array[j] = array[j] - array[0];
Heapify(ref array, j, 0);
}
}
public static void Main()
{
string[] w = Console.ReadLine().Split(" ");
int[] a = new int[w.Length];
for(int i = 0; i < w.Length; i ++)
{
a[i] = Convert.ToInt32(w[i]);
}
Sort(ref a, a.Length);
foreach(int i in a)
{
Console.Write(i);
Console.Write(" ");
}
Console.ReadLine();
}
}
}