简体   繁体   English

在耦合的两个链表上运行mergesort

[英]Running mergesort on two linkedLists that are coupled

I have the following problem and would like to see if somebody could tell me if I'm on the right track with my approach: 我遇到以下问题,想看看是否有人可以告诉我我的方法是否正确:

I want to do a "rolling window" kind of computation on two linked lists and for that I need them to be sorted by magnitude. 我想对两个链表进行“滚动窗口”式的计算,为此,我需要按大小对它们进行排序。 If I just have one linked list, writing the corresponding mergesort is not the problem. 如果我只有一个链表,那么编写相应的mergesort并不是问题。 However, now I'm wondering how I should go about the fact that I have two linked lists where I want to have the corresponding elements from list 1 and list 2 move together as I sort by the magnitude of list 1. If this is not entirely clear, this is what I mean: 但是,现在我想知道我应该如何处理这个事实:我有两个链接列表,当我按照列表1的大小排序时,我希望列表1和列表2中的相应元素一起移动。完全清楚,这就是我的意思:

In list 1, I want to do a sort by magnitude, so basically just rearrange the pointers. 在清单1中,我想按大小进行排序,因此基本上只是重新排列指针。 Whenever I move element "n" in one list, however, I also need to move the corresponding element "n" in the other list to the same position as the element from the other one. 但是,每当在一个列表中移动元素“ n”时,我还需要将另一列表中的相应元素“ n”移动到与另一个列表中的元素相同的位置。

Would my approach of using mergesort for this be the right way to go or does anyone know a better approach? 我将为此使用mergesort的方法是正确的方法,还是有人知道更好的方法? I am having a hard time imagining how I would go about reordering the second list while mergesorting the first one since the second list is not necessarily going to be sorted by magnitude anymore and I need the individual elements to correspond to each other. 我很难想象如何在对第一个列表进行合并排序时如何对第二个列表进行重新排序,因为第二个列表不必再按大小排序了,我需要各个元素相互对应。 Thanks! 谢谢!

Marc

只需创建一对对应元素的列表,然后按该对的第一个元素对列表进行排序即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM