# Merge Sort on Doubly Linked List

Merge sort is a recursive method that splits a list in half repeatedly. The list is sorted by definition if it is empty or contains only one item (the base case). If the list contains more than one item, we divide it in half and recursively perform a merge sort on both portions. The basic procedure, known as a merge, is undertaken after the two halves have been sorted. Merging is integrating two smaller sorted lists into a single sorted new list. If the list's length is less than or equal to one, the list is already sorted, and no further processing is required. However, if the length is larger than one, the left and right halves are extracted using the Python slice method. It's worth noting that the list could not even have many entries. It doesn't matter because the lengths will only differ by one. The following steps show how a merge sort works conceptually:

• Separate the unsorted list into n sublists, each with one entry (a list of one element is considered sorted).
• Merge sublists repeatedly to create new sorted sublists until only one sublist remains. The sorted list will be displayed here.

## Top-down Implementation

The top-down merge sort technique employs indices that recursively split the list (called runs in this example) into sublists until the sublist size is 1, then merges those sublists to generate a sorted list. By rotating the direction of the merging with each recursion level, the copy back step is avoided (except for an initial one-time copy, which can be avoided too).

Consider a two-element array to comprehend this better. The components are transferred to B[] before being combined into A[]. If there are four elements, single element runs from A[] are merged to B[] at the bottom of the recursion level, and two-element runs are merged to A[] at the next higher level of recursion. With each level of recursion, the pattern persists.

### Natural Merge Sort

A natural merge sort is similar to a bottom-up merge sort in that it takes advantage of any naturally occurring runs (sorted sequences) in the input. Lists (or, equivalently, tapes or files) are useful data structures for both monotonic and bitonic (alternating up/down) runs (used as FIFO queues or LIFO stacks). The starting point in a bottom-up merge sort is that each run is one item long.

Natural merge sort is used as the fundamental component of Timsort in many practical instances since long natural runs are present. In actuality, random input data will include a lot of short runs that are sorted by chance. Because there are fewer runs to merge, the natural merge sort may not require as many passes in most cases. In the best-case scenario, the input has already been sorted (i.e., it is a single run). Therefore, the natural merge sort only needs to go over the data once.

### Optimizing Merge Sort

Because multiple memory hierarchies are employed on current computers, the locality of reference can be crucial in program optimization. Cache-aware variants of the merge sort algorithm have been developed, with operations deliberately designed to minimize page movement in and out of a machine's memory cache. For example, the tiled merge sort algorithm stops splitting subarrays when subarrays of size S are achieved. S is the number of data items that fit into a CPU's cache. To avoid memory swaps, each subarray is sorted using an in-place sorting algorithm like insertion sort, and the conventional merge sort is then finished in the traditional recursive method.

Now let's write some codes to perform the sorting operation on the doubly linked list data structure with the help of a merge sort algorithm.

### Java Code

First, let's write a Java code to perform merge sort on a doubly linked list data structure.

Output

The above code gives the following output.

```Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter integer element to insert
23

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter integer element to insert
76

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter integer element to insert
55

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter integer element to insert
20
Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter integer element to insert
89

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
78  1
Enter integer element to insert
78

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
32 1  1
Enter integer element to insert
31

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter integer element to insert
68

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
2
Forward Traversal using next pointer
68 31 78 89 20 55 76 23 Backward Traversal using prev pointer
23 76 55 20 89 78 31 68
Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
3
Quicksort done.

Do you want to continue (Type y or n)

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
2
Forward Traversal using next pointer
20 23 31 55 68 76 78 89 Backward Traversal using prev pointer
89 78 76 68 55 31 23 20
Do you want to continue (Type y or n)

n
```

### C++ Code

Now, let's write a C++ code to perform merge sort on a doubly linked list data structure.

Output

The above C++ code gives this output.

```Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter the value of the node to be inserted
12

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter the value of the node to be inserted
78

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter the value of the node to be inserted
45

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter the value of the node to be inserted
33

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter the value of the node to be inserted
92

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter the value of the node to be inserted
45

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1
Enter the value of the node to be inserted
65

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
2
Contents of the Doubly Linked List are::
Forward Traversal using next pointer
65 45 92 33 45 78 12
Backward Traversal using prev pointer
12 78 45 33 92 45 65
Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
3
Merge sort applied successfully on the Doubly Linked List.

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
2
Contents of the Doubly Linked List are::
Forward Traversal using next pointer
12 33 45 45 65 78 92
Backward Traversal using prev pointer
92 78 65 45 45 33 12
Do you want to continue (Type y or n)
n
```

### C Code

Now, let's write a C code to perform merge sort on a doubly linked list data structure.

Output

The above C code gives the following output.

```Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
22

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
89

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
54

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
32

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
81

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
47

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
8 3

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
1

Enter the value of the node to be inserted
97

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
2

Contents of the Doubly Linked List are::

Forward Traversal using next pointer
97 3 47 81 32 54 89 22
Backward Traversal using prev pointer
22 89 54 32 81 47 3 97
Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
3

Merge sort applied successfully on the Doubly Linked List.

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Merge sort on the Doubly Linked List.
2
Contents of the Doubly Linked List are::

Forward Traversal using next pointer
3 22 32 47 54 81 89 97
Backward Traversal using prev pointer
97 89 81 54 47 32 22 3
Do you want to continue (Type y or n)
n
```

In this way, we have written Java, C++, and C code to perform the merge sort operation on the doubly linked list data structure. There are three functions written for the doubly linked list data structure one is to add a new node in the doubly linked list, the second one is to display all the nodes of the doubly linked list and display the values associated with those nodes, and third and the last function is to perform the merge sort operation on the doubly linked list data structure.

The user first adds a sufficient amount of nodes in the doubly linked list data structure. Once the nodes are added successfully, the merge sort operation is performed on the doubly linked list data structure by selecting the third option from the menu displayed after each operation which will call the mergesort() function written in the code to which pass the root node of the doubly linked list as a parameter.

After the successful completion of the merge sort operation, the user can confirm the result of the quicksort operation by displaying the values of the nodes in the doubly linked list by selecting the second option from the user. Once all the operations are done, the user can exit the code by entering the 'n' or 'N' character.