-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.cs
executable file
·90 lines (81 loc) · 3.04 KB
/
HeapSort.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace SortsBenchmark
{
public static partial class Sorts
{
public static List<int> HeapSort(List<int> numbers, int regularity = 2)
{
// NOTICE: root in the heap is in heap[1] ... heap[0] is unused
List<int> heap = HeapCreate(numbers, regularity);
return HeapPops(heap, regularity);
}
public static List<int> HeapCreate(List<int> numbers, int regularity = 2)
{
// add the "invalid" element to numbers[0]!
numbers.Add(0);
Swap(numbers, 0, numbers.Count - 1);
for (int i = numbers.Count/2; i >0; i--)
{
percolateDown(numbers, regularity, numbers.Count-1, i);
}
return numbers;
}
public static List<int> HeapPops(List<int> heap, int regularity = 2)
{
List<int> sorted = new List<int>();
int heapsize = heap.Count - 1; // index of the last valid element in the heap
for (int i = 0; i < heap.Count - 1; i++)
{
int el = heap[1];
heap[1] = heap[heapsize];
heapsize = heapsize - 1;
// position of a root element is 1
percolateDown(heap, regularity, heapsize, 1);
// place the element at the invalid position
heap[heapsize + 1] = el;
}
heap.RemoveAt(0);
heap.Reverse(); // incredibly fast! is that an O(1) ? :-)
return heap;
}
private static void percolateDown(List<int> heap, int regularity, int heapsize, int pos)
{
bool end = false;
while (!end)
{
int minIndex = -1; //invalid by default
int minValue = int.MaxValue;
//find the child with the smallest value. If it weren't the smallest value - it would make the heap invalid!
for (int j = (pos - 1) * regularity + 2; j < pos * regularity + 2; j++)
{
if (j <= heapsize && heap[j] < (int)heap[pos])
{
if (heap[j] < minValue)
{
minValue = heap[j];
minIndex = j;
}
}
}
//swap myself with the smallest element
if (minIndex != -1)
{
//inlined swap
//Swap(heap, pos, minIndex);
int tmp = heap[pos];
heap[pos] = heap[minIndex];
heap[minIndex] = tmp;
pos = minIndex;
}
else
{
end = true;
}
}
}
}
}