简体   繁体   中英

Stack overflow with merge sort algorithm

I'm trying to order a file with 577000 lines by year. 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

You don't need a complex sorting. Given that you only need to cover about 300 different years this is what I would do.

Make an array[300] of linked list. Index zero is the linked list for the year 1730. Index 1 is the linked list for year 1731.

Now when you read a new entry from the file, you can find the array index by subtracting 1730 from the year read. 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.

Now you have a linked list sorted by year.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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