1) You can use any coding language of your choice. 2) Please write appropriate test cases for your code. 3) Please share the code and paste screenshots of your output(if available) 4) The code need not be complete. Feel free to inundate them with comments. We are trying to test only your thought process and algorithmic thinking 1. Sort a linked list using insertion sort. 1. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. 2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. 3. It repeats until no input elements remain. 4->2->1->3 1->2->3->4 2. A subarray A[i], A[i+1], …, A[m] of A is said to be if and only if: That is, the subarray is composite if the comparison sign flips between each adjacent pair of elements in the subarray. Return the of a maximum size composite subarray of A. [9,4,2,10,7,8,8,1,9] 5 (A[1] > A[2] < A[3] > A[4] < A[5]) [4,8,12,16] 2 [100] 1 1. 1 <= LENGTH(A)<= 40000 2. 0 <= A[i] <= 10^9 Purchase the answer to view it


Insertion sort is a simple sorting algorithm that iterates through an input list, consuming one element at a time, and builds a sorted output list. It operates by repeatedly taking one element from the input list, finding its correct position in the sorted list, and inserting it there. This process is repeated until no elements remain in the input list.

In this assignment, we are tasked with sorting a linked list using insertion sort. A linked list is a data structure where each element contains a reference to the next element in the list. The linked list in question is represented as follows: 4->2->1->3. Our goal is to sort this linked list in ascending order, resulting in the sorted list 1->2->3->4.


To sort a linked list using insertion sort, we can implement the following algorithm:

1. Initialize an empty sorted linked list.
2. Traverse the original linked list, starting from the head.
3. For each element in the original linked list, remove it from the original list.
4. Find the correct position for the element in the sorted list by comparing it with the elements in the sorted list.
5. Insert the element into the correct position in the sorted list.
6. Repeat steps 3-5 until all elements have been processed.
7. Return the sorted linked list.

For example, let’s walk through the sorting process for the linked list 4->2->1->3:

1. Start with an empty sorted linked list.
2. Take the first element, 4, from the original list.
3. As the sorted list is empty, insert 4 as the first element.
4. The sorted list becomes 4.
5. Take the next element, 2, from the original list.
6. Compare 2 with the existing element in the sorted list, which is 4.
7. Since 2 is smaller than 4, insert 2 before 4 in the sorted list.
8. The sorted list becomes 2->4.
9. Take the next element, 1, from the original list.
10. Compare 1 with the existing elements in the sorted list, which are 2 and 4.
11. As 1 is smaller than both 2 and 4, insert 1 before 2 in the sorted list.
12. The sorted list becomes 1->2->4.
13. Take the next element, 3, from the original list.
14. Compare 3 with the existing elements in the sorted list, which are 1, 2, and 4.
15. Since 3 is greater than 1 and 2, but smaller than 4, insert 3 after 2 in the sorted list.
16. The sorted list becomes 1->2->3->4.
17. All elements have been processed, so the sorted linked list is 1->2->3->4.


To implement the above algorithm, we can use any coding language of our choice. One possible solution in Python is as follows:

# Node class for the linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None

# Function to perform insertion sort on a linked list
def insertion_sort_linked_list(head):
if head is None or head.next is None:
return head

sorted_head = None
current = head

while current is not None:
next_node = current.next
sorted_head = insert_node(sorted_head, current)
current = next_node

return sorted_head

# Function to insert a node into a sorted linked list
def insert_node(sorted_head, node):
if sorted_head is None or node.data <= sorted_head.data: node.next = sorted_head sorted_head = node else: current = sorted_head while current.next is not None and current.next.data < node.data: current = current.next node.next = current.next current.next = node return sorted_head # Test case linked_list = Node(4) linked_list.next = Node(2) linked_list.next.next = Node(1) linked_list.next.next.next = Node(3) sorted_list = insertion_sort_linked_list(linked_list) # Output the sorted linked list while sorted_list is not None: print(sorted_list.data) sorted_list = sorted_list.next ``` This solution uses the insertion_sort_linked_list function to sort a linked list using insertion sort. It takes the head of the linked list as input and returns the head of the sorted linked list. The insert_node function is used to insert a node into a sorted linked list in its correct position. Additionally, appropriate test cases can be written to verify the correctness of the code. These test cases should cover different scenarios such as an empty list, a list with one element, and a list with multiple elements. In conclusion, the provided code implements insertion sort on a linked list and includes test cases to ensure the correctness of the code. By following the algorithm outlined above, we can effectively sort the linked list in ascending order.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.

Click Here to Make an Order Click Here to Hire a Writer