-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortUtils.cs
More file actions
46 lines (41 loc) · 1.17 KB
/
Copy pathSortUtils.cs
File metadata and controls
46 lines (41 loc) · 1.17 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
using System.Runtime.CompilerServices;
namespace Sort;
public static class SortUtils
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Swap<T>(IList<T> arr, int i1, int i2)
{
(arr[i2], arr[i1]) = (arr[i1], arr[i2]);
}
public static int PowerOfTwoFloor(int n)
{
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return (n >> 1) + 1;
}
public static void ShowLog<T>(IList<T> arr)
{
string log = "";
foreach (var e in arr)
log += $"{e} ";
Console.WriteLine(log);
}
public static void PriorityQueueDown<T>(IList<T> arr, int parent, int count) where T : IComparable<T>
{
var temp = arr[parent];
for (int child = (parent << 1) + 1; child < count; child = (parent << 1) + 1)
{
if (child + 1 < count && arr[child + 1].CompareTo(arr[child]) > 0)
++child;
if (arr[child].CompareTo(temp) > 0)
arr[parent] = arr[child];
else
break;
parent = child;
}
arr[parent] = temp;
}
}