i have code for mergesort using linked list,it works fine,my question what is complexity of this algorithm?is it O(nlog(n))?also is it stable?i am interested because as i
How not to implement mergesort for linked lists
1
and n - 1
, as explained by ruakhHow to implement mergesort for linked lists
Instead of using bisection, build the lists up by maintaining a stack of already sorted sublists. That is, start by pushing lists of size 1
to the stack and merge down until you reach a list of greater size; you don't actually need to store the list sizes if you can figure out the math behind that.
The sorting algorithm will be stable iff the merge function is. A stable version would build the merged list from scratch by always taking a single element from the lists, and using the first list in case of equality. An unstable, but better performing version would add to the merged list in chunks, avoiding unnecessary re-linking after each element.