对排序的子序列最有效的排序算法

编程入门 行业动态 更新时间:2024-10-10 17:28:18
本文介绍了对排序的子序列最有效的排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我型长(升序)的数数排序的序列,并希望生成包含在同一个订单中所有的元素一个主序列。我寻找最有效的排序算法来解决这个问题。我针对C#.NET 4.0中,因此也欢迎针对并行的想法。

I have several sorted sequences of numbers of type long (ascending order) and want to generate one master sequence that contains all elements in the same order. I look for the most efficient sorting algorithm to solve this problem. I target C#, .Net 4.0 and thus also welcome ideas targeting parallelism.

下面是一个例子: S1 = 1,2,3,5,7,13 S2 = 2,3,6 S3 = 4,5,6,7,8 所得序列= 1,2,2,3,3,4,5,5,6,6,7,7,8,13

Here is an example: s1 = 1,2,3,5,7,13 s2 = 2,3,6 s3 = 4,5,6,7,8 resulting Sequence = 1,2,2,3,3,4,5,5,6,6,7,7,8,13

编辑:当有两个(或更多)相同的值则顺序这两个(或更多个)也没有关系

When there are two (or more) identical values then the order of those two (or more) does not matter.

推荐答案

更​​新:

原来,所有的算法,...这是仍然较快的简单方法:

Turns out that with all the algorithms... It's still faster the simple way:

private static List<T> MergeSorted<T>(IEnumerable<IEnumerable<T>> sortedBunches) { var list = sortedBunches.SelectMany(bunch => bunch).ToList(); list.Sort(); return list; }

和遗留的目的...

下面是最后的版本,优先考虑:

Here is the final version by prioritizing:

private static IEnumerable<T> MergeSorted<T>(IEnumerable<IEnumerable<T>> sortedInts) where T : IComparable<T> { var enumerators = new List<IEnumerator<T>>(sortedInts.Select(ints => ints.GetEnumerator()).Where(e => e.MoveNext())); enumerators.Sort((e1, e2) => e1.Current.CompareTo(e2.Current)); while (enumerators.Count > 1) { yield return enumerators[0].Current; if (enumerators[0].MoveNext()) { if (enumerators[0].Current.CompareTo(enumerators[1].Current) == 1) { var tmp = enumerators[0]; enumerators[0] = enumerators[1]; enumerators[1] = tmp; } } else { enumerators.RemoveAt(0); } } do { yield return enumerators[0].Current; } while (enumerators[0].MoveNext()); }

更多推荐

对排序的子序列最有效的排序算法

本文发布于:2023-11-30 09:48:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最有效   序列   算法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!