# Merge Sort for Doubly Linked List

This is a recursive procedure based on divide and conquer techniques to solve the problem. This means dividing the main problem into smaller ones and merging them later to get a solution to the bigger one. Merge sort splits the list of items into smaller lists until a single item remains in the list, then combines them in the correct order to get the ordered list of items. It is also a classification algorithm based on O (nlogn) complexity. This section describes the type of data structure combination, along with its algorithms and examples.

### Merge sort algorithm:

1. Returns the list if the header is null or if the list has only one element.
2. Divide the original list into two parts to create two lists.
3. Order the first and second parts of the list.
4. Merge both ordered lists

The following is an implementation of the double linked list join sorting.

#### Here Code:

```#include <iostream>
#include <new>
#define SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
using namespace std;
struct node {
int data;
struct node *next;
struct node *prev;
};
node *createList(int *arr, int n){
p = head = new node;
for (int i = 1; i < n; ++i) {
q = new node;
q->data = arr[i];
q->prev = p;
q->next = NULL;
p->next = q;
p = q;
}
}
cout << head->data << " ";
}
cout << endl;
}
node *result = NULL;
}
}
} else {
}
}
void splitList(node *src, node **fRef, node **bRef){
node *fast;
node *slow;
slow = src;
fast = src->next;
while (fast != NULL) {
fast = fast->next;
if (fast != NULL) {
slow = slow->next;
fast = fast->next;
}
}
*fRef = src;
*bRef = slow->next;
slow->next = NULL;
}
node *a = NULL;
node *b = NULL;
if (p == NULL || p->next == NULL) {
return;
}
splitList(p, &a, &b);
mergeSort(&a);
mergeSort(&b);
}
int main(){
int arr[] = {10, 20, 8, 17, 5, 13, 4};
cout << "Unsorted list: " << endl;
cout << "Final sorted list: " << endl;
return 0;
}```

### Complexity Analysis:

• Time Complexity: The time complexity of the above implementation is the same as the time complexity of the array merge classification. Θ (nLogn) It takes time.

### Conclusion:

Merge sort is a divide and conquer algorithm that uses the merge process to combine two subarrays into one by sorting the items out of order. The elements of these subarrays work on the assumption that they are already ordered. This is a stable algorithm that is often used for investment counting and linked list problems, or for external ranking.

### References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

## Practice your Code online here

(Visited 21 times, 1 visits today)

Article Rating
Subscribe
Notify of
Inline Feedbacks

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]

[…] Merge Sort for Doubly Linked List […]