Skip to content

Conversation

@maman
Copy link

@maman maman commented Sep 3, 2020

Summary

This PR adds both implementations and tests for LinkedList.prototype.sort method which can be used as follows:

const list = new LinkedList(2, 1, 3);
list.sort()
// output [1, 2, 3]
list.sort((a, b) => {a.values - b.values})
// output [3, 2, 1];

The default sort method is using mergesort as per ecma262 implementation of Array.prototype.sort, but simplified.

Fixes #3

return merge(mergeSort(left), mergeSort(right));
}

const sortedArray = mergeSort(this.toArray());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not use standard Array since the LinkedList are alternative to dynamic array, not an abstraction on top of it.

there is some example for implementing MergeSort in LinkedList
https://www.geeksforgeeks.org/merge-sort-for-linked-list/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sort Node in LinkedList

2 participants