Which of the following sorting algorithm we can use to sort a linked list keeping the time complexity in mind?

Data Structures | Linked List | Question 4

Which of the following sorting algorithms can be used to sort a random linked list with minimum time complexity?
(A) Insertion Sort
(B) Quick Sort
(C) Heap Sort
(D) Merge Sort

Answer: (D)
Explanation: Both Merge sort and Insertion sort can be used for linked lists.

The slow random-access performance of a linked list makes other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible.

Since worst case time complexity of Merge Sort is O(nLogn) and Insertion sort is O(n^2), merge sort is preferred.

See following for implementation of merge sort using Linked List.

https://www.geeksforgeeks.org/merge-sort-for-linked-list/amp/
Quiz of this Question

Which of the following sorting algorithm we can use to sort a linked list keeping the time complexity in mind?

Article Tags :
Data Structures
Data Structures
Data Structures-Linked List
Linked Lists
Practice Tags :
Data Structures

Using Insertion Sort on a Singly Linked List

Linked lists are among the key topics that software engineers must master before going for a tech interview. Problems on linked lists are pretty popular in coding interviews.

In this article, we will cover a basic problem — how to use insertion sort on a singly linked list.

If you’re a software developer preparing for a technical interview, you would already know that the data structure “singly linked list” is quite similar to the array. The only difference is that data is not stored in a continuous manner in a singly linked list. All the elements are stored as “nodes,” and each node stores some data and pointers to the next node.

The insertion sort is a sorting algorithm that works similar to how you sort playing cards in your hands. We use this algorithm to sort an array in O(n^2) complexity.

Here’s what we’re going to cover in this article:

  • Problem Statement
  • Approach Used to Sort a Singly Linked List Using Insertion Sort
  • Pseudocode for Sorting a Singly Linked List Using Insertion Sort
  • Code for Sorting a Singly Linked List Using Insertion Sort
  • Complexity of the Approach Used
  • FAQs