Skip to content
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

Compiled Merge Sort Code #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 90 additions & 13 deletions 12_LinkedList.java/MergeSort.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,93 @@
import java.util.LinkedList;
public class MergeSort {
public static void mergeSort(LinkedList<Integer> ll) {


import java.util.*;
public class D25_MergeSortLL {
public static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static void main(String args[]) {
LinkedList<Integer> ll = new LinkedList<>();
ll.addLast(4);
ll.addLast(3);
ll.addLast(2);
ll.addLast(1);

System.out.println(ll);
public static Node head;
public static Node tail;
public Node FindMiddle(Node head) {
Node slow = head;
Node fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public Node merge(Node head1, Node head2) {
Node mergedLL = new Node(-1);
Node temp = mergedLL;
while (head1 != null && head2 != null) {
if (head1.data >= head2.data) {
temp.next = head2;
head2 = head2.next;
temp = temp.next;
} else {
temp.next = head1;
head1 = head1.next;
temp = temp.next;
}
}
while (head1 != null) {
temp.next = head1;
head1 = head1.next;
temp = temp.next;
}
while (head2 != null) {
temp.next = head2;
head2 = head2.next;
temp = temp.next;
}
return mergedLL.next;
}
public Node mergeSort(Node head) {
if (head == null || head.next == null) {
return head;
}
Node mid = FindMiddle(head);
Node rightHead = mid.next;
mid.next=null;
Node newleft = mergeSort(head);
Node newRight = mergeSort(rightHead);
return merge(newleft, newRight);
}
public void addFirst(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
}else{
newNode.next = head;
head = newNode;
}
}
public void print_LL() {
if (head == null) {
System.out.println("Linked List is empty");
return;
}
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "=>");
temp = temp.next;
}
System.out.println("null");
}
public static void main(String[] args) {
D25_MergeSortLL LL =new D25_MergeSortLL();
LL.addFirst(1);
LL.addFirst(2);
LL.addFirst(3);
LL.addFirst(4);
LL.addFirst(5);
System.out.println("UNSORTED LINKED LIST");
LL.print_LL();
LL.head=LL.mergeSort(head);
System.out.println("SORTED LINKED LIST - AFTER APPLYING MERGE SORT");
LL.print_LL();
}
}