###### ISO-OSI-layers

October 11, 2023###### Linked-List

October 11, 2023# Data-Structures

Question 4 |

What is the worst case time complexity of inserting n elements into an empty linked list, if the linked list needs to be maintained in sorted order?

θ(n log n) | |

θ(n ^{2}) | |

θ(1) | |

θ(n) |

Question 4 Explanation:

The Linked list insertion operations will take O(1) time. It means a constant amount of time for insertion.

Total number of elements inserted into an empty linked list is O(n). So, it will take O(n) time in the worst case.

After inserting elements into an empty linked list we have to perform sorting operation.

To get minimum time complexity to perform sorting order is merge sort. It will give O(nlogn) time complexity only.

Let head be the first node of the linked list to be sorted and head Reference be the pointer to head.

The head in MergeSort as the below implementation changes next links to sort the linked lists (not data at the nodes), so head node has to be changed if the data at the original head is not the smallest value in the linked list.

Note: There are other sorting methods also will give decent time complexity but quicksort will give O(n

Total number of elements inserted into an empty linked list is O(n). So, it will take O(n) time in the worst case.

After inserting elements into an empty linked list we have to perform sorting operation.

To get minimum time complexity to perform sorting order is merge sort. It will give O(nlogn) time complexity only.

Let head be the first node of the linked list to be sorted and head Reference be the pointer to head.

The head in MergeSort as the below implementation changes next links to sort the linked lists (not data at the nodes), so head node has to be changed if the data at the original head is not the smallest value in the linked list.

Note: There are other sorting methods also will give decent time complexity but quicksort will give O(n

^{2}) and heap sort will not be suitable to apply.Correct Answer: B

Question 4 Explanation:

The Linked list insertion operations will take O(1) time. It means a constant amount of time for insertion.

Total number of elements inserted into an empty linked list is O(n). So, it will take O(n) time in the worst case.

After inserting elements into an empty linked list we have to perform sorting operation.

To get minimum time complexity to perform sorting order is merge sort. It will give O(nlogn) time complexity only.

Let head be the first node of the linked list to be sorted and head Reference be the pointer to head.

The head in MergeSort as the below implementation changes next links to sort the linked lists (not data at the nodes), so head node has to be changed if the data at the original head is not the smallest value in the linked list.

Note: There are other sorting methods also will give decent time complexity but quicksort will give O(n

Total number of elements inserted into an empty linked list is O(n). So, it will take O(n) time in the worst case.

After inserting elements into an empty linked list we have to perform sorting operation.

To get minimum time complexity to perform sorting order is merge sort. It will give O(nlogn) time complexity only.

Let head be the first node of the linked list to be sorted and head Reference be the pointer to head.

The head in MergeSort as the below implementation changes next links to sort the linked lists (not data at the nodes), so head node has to be changed if the data at the original head is not the smallest value in the linked list.

Note: There are other sorting methods also will give decent time complexity but quicksort will give O(n

^{2}) and heap sort will not be suitable to apply. Subscribe

Login

0 Comments