use*_*998 2 arrays sorting algorithm
我有一千个类型的对象 MyClass
class MyClass{
array<MyClass> objectsBehind;
Boolean large;
}
Run Code Online (Sandbox Code Playgroud)
对象objectsBehind
数组在哪里MyClass
,此数组中的任何对象都是1000个原始对象的一部分.
我将它们放在一个数组中并对它们进行排序,使得一个对象出现在数组中比其objectsBehind
数组中的对象更高的索引处.例如,索引546处的对象不能在其objectsBehind
数组中具有排序数组中索引大于546的任何对象.
我的问题是这个.1000件物品中约有20件拥有该物业large = true
.如果objectsBehind
不违反属性,则在排序数组中按顺序对这些"大"对象进行分组是有益的.例如,最好是这样:
array[45].large = false; array[46].large = true, array[47].large = true, array[48].large = true, array[49].large = false;
Run Code Online (Sandbox Code Playgroud)
比
array[45].large = true; array[46].large = false, array[47].large = true, array[48].large = false, array[49].large = true;
Run Code Online (Sandbox Code Playgroud)
在第一个序列中,我将三个大对象组合在一起,而不是在第二个示例中将它们展开.
我想不出这样做的好方法.有任何想法吗?
这可能很容易完成.
首先,要在objectsBehind
每个此类对象的数组中的所有对象之前对对象进行排序,您将使用拓扑排序.
拓扑排序将每次主要迭代"同时"将多个元素放入输出集合中.
这些对象按大属性排序,这样所有大对象首先出现,然后是所有非大对象.您可能希望在此处添加另一个排序条件,以便您可以依赖大对象内部和非大对象内的已知顺序.
基本上,这是拓扑排序的工作原理(我学到的):
objectsBehind
属性中内置了它)如果在第8点和第4点之后,您在散列集中没有对象,但是第二个字典中的对象没有达到零入站链接,则意味着您在图中有循环,即.沿着"物体1指向物体2,物体2指向3,3点指向1"的东西.
这是一个这样的解决方案,你可以在LINQPad中测试它.
请注意,有很多方法可以进行拓扑排序.例如,此版本将采用之前没有对象的所有对象,并同时输出这些对象.但是,您可以直接在它们之后的对象之后直接对其中一些对象进行分组,并且仍然不违反任何约束.
例如,在下面的代码中查看4和7之间的关系(您必须运行它).
const int OBJECT_NUM = 10;
void Main()
{
Random r = new Random(12345);
var objects = new List<MyClass>();
for (int index = 1; index <= OBJECT_NUM; index++)
{
var mc = new MyClass { Id = index, IsLarge = (r.Next(100) < 50) };
objects.Add(mc);
}
for (int index = 0; index < objects.Count; index++)
{
var temp = new List<MyClass>();
for (int index2 = index + 1; index2 < objects.Count; index2++)
if (r.Next(100) < 10 && index2 != index)
temp.Add(objects[index2]);
objects[index].ObjectsBehind = temp.ToArray();
}
objects.Select(o => o.ToString()).Dump("unsorted");
objects = LargeTopoSort(objects).ToList();
objects.Select(o => o.ToString()).Dump("sorted");
}
public static IEnumerable<MyClass> LargeTopoSort(IEnumerable<MyClass> input)
{
var inboundLinkCount = new Dictionary<MyClass, int>();
var inputArray = input.ToArray();
// the hash set initially contains all the objects
// after the first loop here, it will only contain objects
// that has no inbound links, they basically have no objects
// that comes before them, so they are "first"
var objectsWithNoInboundLinks = new HashSet<MyClass>(inputArray);
foreach (var source in inputArray)
{
int existingInboundLinkCount;
foreach (var target in source.ObjectsBehind)
{
// now increase the number of inbound links for each target
if (!inboundLinkCount.TryGetValue(target, out existingInboundLinkCount))
existingInboundLinkCount = 0;
existingInboundLinkCount += 1;
inboundLinkCount[target] = existingInboundLinkCount;
// and remove it from the hash set since it now has at least 1 inbound link
objectsWithNoInboundLinks.Remove(target);
}
}
while (objectsWithNoInboundLinks.Count > 0)
{
// all the objects in the hash set can now be dumped to the output
// collection "at the same time", but let's order them first
var orderedObjects =
(from mc in objectsWithNoInboundLinks
orderby mc.IsLarge descending, mc.Id
select mc).ToArray();
foreach (var mc in orderedObjects)
yield return mc;
// prepare for next "block" by clearing the hash set
// and removing all links from the objects we just output
objectsWithNoInboundLinks.Clear();
foreach (var source in orderedObjects)
{
foreach (var target in source.ObjectsBehind)
{
if (--inboundLinkCount[target] == 0)
{
// we removed the last inbound link to an object
// so add it to the hash set so that we can output it
objectsWithNoInboundLinks.Add(target);
}
}
}
}
}
public class MyClass
{
public int Id; // for debugging in this example
public MyClass[] ObjectsBehind;
public bool IsLarge;
public override string ToString()
{
return string.Format("{0} [{1}] -> {2}", Id, IsLarge ? "LARGE" : "small", string.Join(", ", ObjectsBehind.Select(o => o.Id)));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
unsorted
1 [LARGE] -> 5
2 [LARGE] ->
3 [small] ->
4 [small] -> 7
5 [small] ->
6 [small] ->
7 [LARGE] ->
8 [small] ->
9 [LARGE] -> 10
10 [small] ->
sorted
1 [LARGE] -> 5
2 [LARGE] ->
9 [LARGE] -> 10
3 [small] ->
4 [small] -> 7
6 [small] ->
8 [small] ->
7 [LARGE] ->
5 [small] ->
10 [small] ->
Run Code Online (Sandbox Code Playgroud)