-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.cs
73 lines (61 loc) · 1.93 KB
/
Algorithms.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
namespace Utility
{
public static class Algorithms
{
public static void Swap<T>(ref T x, ref T y)
{
T tempswap = x;
x = y;
y = tempswap;
}
private static int qpartition(Edge[] arr, int low, int high)
{
Edge temp;
Edge pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j].Similarity <= pivot.Similarity)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
// Implementation of QuickSelect
public static float[] k2thSmallest(ref Edge[] a, int left, int right, int k)
{
float[] s = new float[2];
int temp = 0;
while (left <= right)
{
// Partition a[left..right] around a pivot
// and find the position of the pivot
int pivotIndex = qpartition(a, left, right);
// If pivot itself is the k-th smallest element
if (pivotIndex == k - 1)
{
s[1] = a[pivotIndex].Similarity;
s[0] = a[temp].Similarity;
return s;
}
// If there are more than k-1 elements on
// left of pivot, then k-th smallest must be
// on left side.
else if (pivotIndex > k - 1)
right = pivotIndex - 1;
// Else k-th smallest is on right side.
else
left = pivotIndex + 1;
temp = pivotIndex;
}
return null;
}
}
}