'Insertion sorting's time complexity in Linked List

void nodeinsert(NODE*node, int p){
NODE * new = (NODE*)malloc(sizeof(NODE));
new->data = p;
new->next = node->next;
node->next = new;}

void insert_sort_list(NODE*head, int*arr, int num_arr){
NODE *temp;
int i;
insert_node(head, arr[0]);
    for(i = 1; i< num_arr; i++){
        temp = head;
        while(temp->next != NULL){
            if(temp->next->data < arr[i]){
                temp = temp->next;
            }
            else{break;}
        }
        nodeinsert(temp, arr[i]);}

this is my code for sorting list using Insertion sort.

I heard Insertion sorting's time complexity is O(n^2) and when I measure sorting time with array using insertion sort, O(n^2)is correct. But when I measure time for this code it does not look like O(n^2). when n is less than 20000 it is fit to O(n^2) but over 20000 it takes more time. I want to know why does the time complexity look diffrent on list.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source