-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add Merge Sort Linked List algorithm #11695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hardvan
wants to merge
18
commits into
TheAlgorithms:master
Choose a base branch
from
Hardvan:merge_sort_linked_list_new_algo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
121bfda
Add Merge Sort Linked List algorithm
Hardvan 7ee4af8
updating DIRECTORY.md
Hardvan 809e74b
Fix comments
Hardvan 53d22e5
Merge branch 'merge_sort_linked_list_new_algo' of https://github.com/…
Hardvan 4c53ee9
Fix
Hardvan 42a1f27
Add return type hint
Hardvan b3304eb
Fix type hint
Hardvan 81d2d51
Fix
Hardvan 6211693
Fix
Hardvan 5556acb
Fix
Hardvan 4aa953e
Fix
Hardvan 62d2412
Fix
Hardvan d2b3c1d
Fix
Hardvan fb13521
Fix
Hardvan 1ac1f0c
Fix doctests
Hardvan ed62d33
Fix
Hardvan a7b312c
Merge branch 'master' into merge_sort_linked_list_new_algo
Hardvan e333efd
updating DIRECTORY.md
Hardvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
class Node: | ||
""" | ||
A class representing a node in a linked list. | ||
|
||
Attributes: | ||
data (int): The data stored in the node. | ||
next (Node | None): A reference to the next node in the linked list. | ||
|
||
>>> head = Node(4) | ||
>>> head.next = Node(2) | ||
>>> head.next.next = Node(1) | ||
>>> head.next.next.next = Node(3) | ||
>>> sorted_head = merge_sort_linked_list(head) | ||
>>> print_linked_list(sorted_head) | ||
1 2 3 4 | ||
""" | ||
|
||
def __init__(self, data: int): | ||
self.data = data | ||
self.next: Node | None = None | ||
|
||
|
||
def get_middle(head: Node) -> Node: | ||
""" | ||
Find the middle node of the linked list using the slow and fast pointer technique. | ||
|
||
Parameters: | ||
head: The head node of the linked list. | ||
|
||
Returns: | ||
The middle node of the linked list. | ||
|
||
Example: | ||
>>> head = Node(1) | ||
>>> head.next = Node(2) | ||
>>> head.next.next = Node(3) | ||
>>> get_middle(head).data | ||
2 | ||
""" | ||
|
||
if head is None: | ||
return head | ||
|
||
slow = head # one node at a time | ||
fast = head # two nodes at a time | ||
while fast.next and fast.next.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
return slow | ||
|
||
|
||
def merge(left: Node | None, right: Node | None) -> Node | None: | ||
""" | ||
Merge two sorted linked lists into one sorted linked list. | ||
|
||
Parameters: | ||
left: The head of the first sorted linked list. | ||
right: The head of the second sorted linked list. | ||
|
||
Returns: | ||
The head of the merged sorted linked list. | ||
|
||
Example: | ||
>>> left = Node(1) | ||
>>> left.next = Node(3) | ||
>>> right = Node(2) | ||
>>> right.next = Node(4) | ||
>>> merged = merge(left, right) | ||
>>> print_linked_list(merged) | ||
1 2 3 4 | ||
""" | ||
|
||
if left is None: | ||
return right | ||
if right is None: | ||
return left | ||
|
||
if left.data <= right.data: | ||
result = left | ||
result.next = merge(left.next, right) | ||
else: | ||
result = right | ||
result.next = merge(left, right.next) | ||
|
||
return result | ||
|
||
|
||
def print_linked_list(head: Node | None) -> None: | ||
""" | ||
Print the linked list in a single line. | ||
|
||
Parameters: | ||
head: The head node of the linked list. | ||
|
||
Example: | ||
>>> head = Node(1) | ||
>>> head.next = Node(2) | ||
>>> head.next.next = Node(3) | ||
>>> print_linked_list(head) | ||
1 2 3 | ||
""" | ||
|
||
current = head | ||
first = True # To avoid printing space before the first element | ||
while current: | ||
if not first: | ||
print(" ", end="") | ||
print(current.data, end="") | ||
first = False | ||
current = current.next | ||
print() | ||
|
||
|
||
def merge_sort_linked_list(head: Node | None) -> Node | None: | ||
""" | ||
Sort a linked list using the Merge Sort algorithm. | ||
|
||
Parameters: | ||
head: The head node of the linked list to be sorted. | ||
|
||
Returns: | ||
The head node of the sorted linked list. | ||
|
||
Example: | ||
>>> head = Node(4) | ||
>>> head.next = Node(2) | ||
>>> head.next.next = Node(1) | ||
>>> head.next.next.next = Node(3) | ||
>>> sorted_head = merge_sort_linked_list(head) | ||
>>> print_linked_list(sorted_head) | ||
1 2 3 4 | ||
|
||
>>> head = Node(1) | ||
>>> head.next = Node(2) | ||
>>> head.next.next = Node(3) | ||
>>> head.next.next.next = Node(4) | ||
>>> sorted_head = merge_sort_linked_list(head) | ||
>>> print_linked_list(sorted_head) | ||
1 2 3 4 | ||
|
||
>>> head = Node(10) | ||
>>> head.next = Node(3) | ||
>>> head.next.next = Node(5) | ||
>>> head.next.next.next = Node(1) | ||
>>> sorted_head = merge_sort_linked_list(head) | ||
>>> print_linked_list(sorted_head) | ||
1 3 5 10 | ||
""" | ||
|
||
# Base Case: 0 or 1 node | ||
if head is None or head.next is None: | ||
return head | ||
|
||
# Split the linked list into two halves | ||
middle = get_middle(head) | ||
next_to_middle = middle.next | ||
middle.next = None # Split the list into two parts | ||
|
||
left = merge_sort_linked_list(head) # Sort left half | ||
right = merge_sort_linked_list(next_to_middle) # Sort right half | ||
|
||
# Merge sorted halves | ||
if left is None: | ||
return right | ||
if right is None: | ||
return left | ||
sorted_list = merge(left, right) | ||
return sorted_list | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.