

Hence, there is zero shiftings and total n comparisions. Pass 5: number of comparision: 1, number of Shift: 0, Pass 2: number of comparision: 1, number of Shift: 0, Pass 1: number of comparision: 1, number of Shift: 0, For example, if the input list: 2, 4, 6, 8, 10, then, Suppose we need to find the ascending order and given input list is also in ascending order then there will be 0(zero) shifting in 'S'. Analysis of Best Case Time Complexity of Insertion Sort Therefore, Worst case time complexity is O(n^2). Hence, Number of comparision: n*(n-1)/2, number of Shift: n*(n-1)/2. Pass 5: number of comparision: 5, number of Shift: 5, Pass 2: number of comparision: 2, number of Shift: 2, Pass 1: number of comparision: 1, number of Shift: 1,

For example, if the input list is 8, 6, 5, 4, 2, then, Suppose we need to arrange list in ascending order and given the input list is decreasing order then there will be maximum number of shiftings in sorted list 'S' for every element. Analysis of Worst Case Time Complexity of Insertion Sort Hence, the time complexity of insertion sort on linked list is O(n^2). Time Complexity of function is depending on Number of comparisions + shifts.

Hence, Total number of comparisions: (n*(n-1))/2 Pass n: number of comparision: n, number of Shift: n. Pass 3: number of comparision: 3, number of Shift: 3 Pass 2: number of comparision: 2, number of Shift: 2 Pass 1: number of comparision: 1, number of Shift: 1 Time Complexity Analysis of Insertion Sort on linked listĪfter each pass, the size of sorted list 'S' increases by 1 and size of unsorted input list decreases by 1. Here, we are appling the idea of putting the element from input list to new sorted node 'S', using comparision of elements and shifting of elements. Temp = temp->next //updating temp to temp's next While ( temp->next & temp->next->value value) //checking conditions Temp = S //initializing temp new node and assigning it S X = root_node->next //intializing x new node and assigning root-node's next node in it S = NULL //initializing sorted linked list and assigning Null
TIME COMPLEXITY LINKED LIST STACK CODE
The Code of Insertion Sort on Linked List is as follows: //Insertion Sortīegin insertion_sort ( root_node ) //root_node is pointing at input list Therefore, lists will look like, Input list: Similarly, after some iterations, Input list will be empty and S will have all elements of initial Input list. Now, compare the first node value of 'Input list' with the node values in 'S' and place the value in S according to sorted order. Now, lets start doing the comparisions, if the S list is empty then insert the first node of Input list into S Input list: Take a new list(let name of new list be S) which will our sorted list and the given input unsorted list.

Insertion sort begins with a single element and iteratively takes in consecutive elements one by one and inserts them into the sorted list on its place without disturbing the sorted order. This method is preferred to use when the elements are almost sorted. The key idea of Insertion sort on linked list algorithm is to insert an node/element into a sorted linked list. Insertion sort is a comparison based sorting algorithm. Overview of Insertion Sort Algorithm on linked list
