Program to sort the elements of the singly linked listExplanationIn this program, we need to sort the nodes of the given singly linked list in ascending order. Show
Original list: Sorted list: To accomplish this task, we maintain two pointers: current and index. Initially, current point to head node and index will point to node next to current. Traverse through the list till current points to null, by comparing current's data with index's data. If current's data is greater than the index's data, then swap data between them. In the above example, current will initially point to 9 and index will point to 7. Since, 9 is greater than 7, swap the data. Continue this process until the entire list is sorted in ascending order. Algorithm
SolutionPythonOutput: Original list:
9 7 2 5 4
Sorted list:
2 4 5 7 9
COutput: Original list:
9 7 2 5 4
Sorted list:
2 4 5 7 9
JAVAOutput: Original list:
9 7 2 5 4
Sorted list:
2 4 5 7 9
C#Output: Original list:
9 7 2 5 4
Sorted list:
2 4 5 7 9
PHPOutput: Original list:
9 7 2 5 4
Sorted list:
2 4 5 7 9
Insertion Sort for Singly Linked ListWe have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Recommended: Please try your approach on {IDE} first, before moving on to the solution. The main step is (2.a) which has been covered in the below post. Below is implementation of above algorithm C
C++
Java
Python
C#
Javascript
Output: Linked List before sorting 30 3 4 20 5 Linked List after sorting 3 4 5 20 30Time and space complexity analysis:In worst case we might have to traverse all nodes of the sorted list for inserting a node. And there are “n” such nodes. Thus Time Complexity: O(n)*O(n)=O(n^2) Space Complexity: No extra space is required depending on the size of the input. Thus Space complexity is constant- O(1). Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Article Tags :
Linked List
Practice Tags :
Linked List Merge Sort for Linked ListsMerge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Let the head be the first node of the linked list to be sorted and headRef be the pointer to head. Note that we need a reference to head in MergeSort() as the below implementation changes next links to sort the linked lists (not data at the nodes), so the head node has to be changed if the data at the original head is not the smallest value in the linked list. MergeSort(headRef) 1) If the head is NULL or there is only one element in the Linked List then return. 2) Else divide the linked list into two halves. FrontBackSplit(head, &a, &b); /* a and b are two halves */ 3) Sort the two halves a and b. MergeSort(a); MergeSort(b); 4) Merge the sorted a and b (using SortedMerge() discussed here) and update the head pointer using headRef. *headRef = SortedMerge(a, b);Linked List Operations: Traverse, Insert and DeleteIn this tutorial, you will learn different operations on a linked list. Also, you will find implementation of linked list operations in C/C++, Python and Java. There are various linked list operations that allow us to perform different actions on linked lists. For example, the insertion operation adds a new element to the linked list. Here's a list of basic linked list operations that we will cover in this article.
Before you learn about linked list operations in detail, make sure to know about Linked List first. Things to Remember about Linked List
In all of the examples, we will assume that the linked list has three nodes 1 --->2 --->3 with node structure as below: struct node { int data; struct node *next; };How to sort a linked list using merge sortMerge sort is one of the most famous divide-and-conquer sorting algorithms. This algorithm can be used to sort values in any traversable data structure (i.e., a linked list). Data Structure and Algorithms - Linked ListAdvertisements Previous Page Next Page A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List.
Recommended: Please try your approach on {IDE} first, before moving on to the solution.The main step is (2.a) which has been covered in below post. Below is implementation of above algorithm C++
|