-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.cs
More file actions
133 lines (114 loc) · 4.53 KB
/
Copy pathHeapSort.cs
File metadata and controls
133 lines (114 loc) · 4.53 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BarcodeRecoveryCS
{
class HeapSort
{
public void Heap_Sort(FoundBars[] array)
{
Heap_Sort(array, 0, array.Length, Comparer<int>.Default);
}
public static void Heap_Sort(FoundBars[] array, int offset, int length, IComparer<int> comparer)
{
Heap_Sort(array, offset, length, comparer.Compare);
}
public static void Heap_Sort(FoundBars[] array, int offset, int length, Comparison<int> comparison)
{
// build binary heap from all items
for (int i = 0; i < length; i++)
{
int index = i;
FoundBars item = array[offset + i]; // use next item
// and move it on top, if greater than parent
while (index > 0 && comparison(array[offset + (index - 1) / 2].x, item.x) < 0)
{
int top = (index - 1) / 2;
array[offset + index] = array[offset + top];
index = top;
}
array[offset + index] = item;
}
for (int i = length - 1; i > 0; i--)
{
// delete max and place it as last
FoundBars last = array[offset + i];
array[offset + i] = array[offset];
int index = 0;
// the last one positioned in the heap
while (index * 2 + 1 < i)
{
int left = index * 2 + 1, right = left + 1;
if (right < i && comparison(array[offset + left].x, array[offset + right].x) < 0)
{
if (comparison(last.x, array[offset + right].x) > 0) break;
array[offset + index] = array[offset + right];
index = right;
}
else
{
if (comparison(last.x, array[offset + left].x) > 0) break;
array[offset + index] = array[offset + left];
index = left;
}
}
array[offset + index] = last;
}
}
}
public class HeapSortSize
{
public void Heap_Sort(List<FoundBars> array)
{
Heap_Sort(array, 0, array.Count, Comparer<int>.Default);
}
public static void Heap_Sort(List<FoundBars> array, int offset, int length, IComparer<int> comparer)
{
Heap_Sort(array, offset, length, comparer.Compare);
}
public static void Heap_Sort(List<FoundBars> array, int offset, int length, Comparison<int> comparison)
{
// build binary heap from all items
for (int i = 0; i < length; i++)
{
int index = i;
FoundBars item = array[offset + i]; // use next item
// and move it on top, if greater than parent
while (index > 0 && comparison(array[offset + (index - 1) / 2].barWidth, item.barWidth) < 0)
{
int top = (index - 1) / 2;
array[offset + index] = array[offset + top];
index = top;
}
array[offset + index] = item;
}
for (int i = length - 1; i > 0; i--)
{
// delete max and place it as last
FoundBars last = array[offset + i];
array[offset + i] = array[offset];
int index = 0;
// the last one positioned in the heap
while (index * 2 + 1 < i)
{
int left = index * 2 + 1, right = left + 1;
if (right < i && comparison(array[offset + left].barWidth, array[offset + right].barWidth) < 0)
{
if (comparison(last.barWidth, array[offset + right].barWidth) > 0) break;
array[offset + index] = array[offset + right];
index = right;
}
else
{
if (comparison(last.barWidth, array[offset + left].barWidth) > 0) break;
array[offset + index] = array[offset + left];
index = left;
}
}
array[offset + index] = last;
}
}
}
}