# Insertion Sort For Singly Linked List – With Example

As part of its basic sorting algorithm, insertion sort repeatedly moves a fresh element from an unsorted list into the appropriate location in the sorted portion of the list. Playing cards can be used to illustrate this, with the algorithm choosing a new card from the deck and placing it in the appropriate spot in the hand each time. The implementation of insertion sort for a singly linked list will be the main topic of this essay. Let us know about the Insertion Sort For Singly Linked List.

Each node in a singly linked list includes a value and a pointer to the node after it in the list. In contrast to a doubly linked list, which contains references to both the previous and next nodes in the list, a single linked list only includes a single reference to the next node in the list.

for the singly linked lists using insertion sort,

## We need to follow the following steps

1. Make a dummy node: To begin the sorted list, make a dummy node that acts as the list’s head. This dummy node makes it possible to insert nodes into the sorted list at the appropriate place.

2. Go through the linked list iteratively: Starting at the head of the list, remove each node one at a time and add them to the sorted list.

3. Identify the ideal node position: To determine the ideal node position, compare the node with each node in the sorted list until you identify a node that is greater than or equal to it.

4. Insert the node: After determining the ideal placement, put the node in front of the one that is more than or equal to it.

5. Continue the procedure: Repeat this procedure until every node has been added to the sorted list from the original linked list and deleted from it.

It is crucial to remember that although the insertion sort performed to a singly linked list is straightforward to comprehend and implement, in the worst case it has an O(n2) time complexity, where n is the number of nodes in the linked list. Because of this, it is a good option for small data sets or partially sorted data, but it is less effective for large data sets, where more effective algorithms like merge sort or rapid sort are frequently utilized.

### The following is an example applied to the list [5, 2, 9, 1, 4, 6]

1.  Set the first node in the sorted section of the list to be a dummy node with a value of infinity.

2. Compare the first node in the sorted section of the list with the second node,

which has a value of 2.

3. Browse the list’s sorted section to determine where the new node should go. In this instance, the new node needs to be added to the sorted section of the list after the fake node and before the first node.

4.  Update the pointers after properly positioning the new node. The new node should now point to the first node in the sorted section of the list, and the fake node should now point to the new node.

5.  Repeat steps 2-4 for each of the list’s remaining nodes. The list’s sorted section should now appear as follows: [infinity, 2, 5, 9, 1, 4, 6].

6.  Return the fictitious node as the list’s head.

1. Simpleness: It is simple to comprehend & use, making it a fantastic choice for novices and students.

2. Flexibility: short lists or lists that have already been partially sorted. Since the input data may vary often in real-time applications, this makes it a desirable option.

3. Stable: The relative order of equal entries in the list is maintained using the stable sorting technique known as insertion sort. This is crucial in applications where the order in which entries with the same value appear in the original list must be maintained.

4. Memory Efficiency: it needs one additional variable to keep track of the node now being processed, hence it uses a constant amount of memory. This makes it a good option for contexts with limited memory.

1. Slow Performance: The time required to sort the elements increases quadratically with the size of the data set since it has a time complexity of O(n2). Because of this, it is inappropriate for processing huge data sets where performance is important.

2.  Large Data Sets Unsuitable: Because the insertion sort’s time complexity is O(n2), it is not effective for sorting large data sets because it will take a long time to finish the sort operation. The use of quicker sorting methods like merge sort or rapid sort is advised for huge data sets.

3. Inefficient for Partially Sorted Data: It is inefficient for partially sorted data because it takes longer to arrange the elements correctly. For partially sorted data, employ adaptive sorting algorithms like bubble sort or selection sort.

4. Not Adaptive: It does not change how it sorts the data in response to new information. As a result, it is less efficient than adaptive sorting algorithms since it does not take use of the data collection’s natural ordering.

5. Limited to Singly Linked Lists: It may only be used to sort singly linked lists; arrays and other data structures cannot be sorted using this method. Compared to other sorting algorithms that can be used to sort various data structures, this limits its adaptability.

#### Conclusion

In conclusion, insertion sort is a simple sorting algorithm that is suitable for small data sets and for use in educational contexts where its ease of implementation and understanding is a priority. However, for large data sets, or in real-world applications where performance is critical, it is recommended to use faster and more efficient sorting algorithms such as quick sort, merge sort, or heap sort. Additionally, insertion sort is limited to singly linked lists and is not an in-place sorting algorithm, making it less versatile than other sorting algorithms.

Insertion Sort For Singly Linked List – With Example
Scroll to top