简体   繁体   English

使用合并排序算法的堆栈溢出

[英]Stack overflow with merge sort algorithm

I'm trying to order a file with 577000 lines by year. 我试图按年订购577000行的文件。 I'm putting the information of which line in a doubly linked list. 我将哪一行的信息放入双链表中。 To order the file I tried to implement the merge sort algorithm , but because the algorithm is recursive when ordering a large quantity of information the algorithm doesn´t work because of the overflow of the stack memory. 为了订购文件,我尝试实现合并排序算法,但是由于该算法在订购大量信息时是递归的,因此由于堆栈内存溢出,该算法无法正常工作。 Can someone help try to find a more efficient algorithm that doesn´t crash and doesn´t take too long ? 有人可以帮忙寻找一种不会崩溃且不会花费太长时间的更有效的算法吗?

From a comment: 来自评论:

The dates ranger from 1730 to 2013 日期范围从1730年到2013年

You don't need a complex sorting. 您不需要复杂的排序。 Given that you only need to cover about 300 different years this is what I would do. 鉴于您只需要涵盖大约300个不同的年份,这就是我要做的。

Make an array[300] of linked list. 制作一个链表的array[300] Index zero is the linked list for the year 1730. Index 1 is the linked list for year 1731. 索引零是1730年的链表。索引1是1731年的链表。

Now when you read a new entry from the file, you can find the array index by subtracting 1730 from the year read. 现在,当您从文件中读取新条目时,可以通过从读取的年份中减去1730来找到数组索引。 Then you add the element to the linked list at that index. 然后,将元素添加到该索引的链表中。

When the whole file has been read, you put all the linked list together to a single linked list starting from index 0. 读取整个文件后,将所有链接列表放到一个从索引0开始的链接列表中。

Now you have a linked list sorted by year. 现在,您具有按年份排序的链接列表。

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

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