Hi, I implemented the cluster by my own method. the logic is that it will first try to find out top n delta (differences), then split the data according to the delta.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication612
{
class Program
{
private class ValueSort : IComparer<int[]>
{
public int Compare(int[] x, int[] y)
{
if (x[1] > y[1])
return -1;
else if (x[1] < y[1])
return 1;
else
return 0;
}
}
private class IndexSort : IComparer<int[]>
{
public int Compare(int[] x, int[] y)
{
if (x[0] > y[0])
return 1;
else if (x[0] < y[0])
return -1;
else
return 0;
}
}
static void Main2(string[] args)
{
int[] data = new int[] { 1, 2, 1, 3, 2, 4, 5, 11, 10, 12, 10, 12, 13, 25, 21, 24, 20, 4, 5, 2, 3 };
int[][] delta = new int[data.Length][];
delta[0] = new int[2];
delta[0][0] = 0;
delta[0][1] = 0;
for (int i = 0; i < data.Length - 1; i++)
{
delta[i + 1] = new int[2];
delta[i + 1][0] = i + 1;
delta[i + 1][1] = Math.Abs(data[i + 1] - data[i]);
}
Array.Sort(delta, new ValueSort());
int groups = 4;
int[][] topDelta = new int[groups - 1][];
for (int i = 0; i < topDelta.Length; i++)
{
topDelta[i] = delta[i];
}
Array.Sort(topDelta, new IndexSort());
List<List<int>> output = new List<List<int>>();
int index = 0;
for (int i = 0; i < topDelta.Length; i++)
{
List<int> group = new List<int>();
for (int j = index; j < topDelta[i][0]; j++)
{
group.Add(data[j]);
}
index = topDelta[i][0] + 1;
output.Add(group);
}
List<int> group2 = new List<int>();
for (int j = topDelta[topDelta.Length - 1][0]; j < data.Length; j++)
{
group2.Add(data[j]);
}
output.Add(group2);
for (int i = 0; i < output.Count; i++)
{
Console.Write("Group" + i + ":");
foreach (int value in output[i])
{
Console.Write(value + ",");
}
Console.WriteLine();
}
}
}
}