 Hello everyone, my name is Nikhil Gadjam, working as assistant professor in department of computer science and engineering in Valchand instead of technology, in today's video we will see a program for sorting a singly linked list using C. These are learning outcome. At the end of this session, student will be able to write a C program for sorting singly linked list. Now this is a singly linked list, linked list can be defined as a collection of nodes and each node in singly linked list consists of only two fields. First one is integer data or we call it as key, second one is a pointer which is pointing to a next node or which holds the address of next node. So in general linked list looks like this, a first node here is tan and start is a global pointer which is pointing to first node tan and last node's next pointer will always store null indicating that this is the end of singly linked list. Now what are the difficulties arise when we sort a singly linked list? Suppose a singly linked list, this is a singly linked list. First problem with singly linked list is singly linked list consists of only one pointer and which points to a next node. So only forward direction traversal is possible, reverse direction traversal is not possible as it is in case of doubly linked list. There are multiple sorting algorithms we have out of which selection sort and bubble sort we can apply for sorting of singly linked list. What happen if we apply selection sort on singly linked list for sorting? So in selection sort each node is compared with remaining all nodes and swapping is done if data present at first node if that is greater than second node then swapping is done like this wise we can apply selection sort. So after each iteration one by one node is getting sorted. What happens if we apply bubble sort is in bubble sort two consecutive nodes or data are compared with each other. So first tan and 20 will be compared, 20 and 30 will be compared, 30 and 40 will be compared and 40 and 50 will be compared. So after one iteration a maximum node will be stored at the last and after second iteration last two nodes will be getting sorted. Now problem with bubble sort is every time we have to keep track as what nodes is getting sorted in previous iteration. So better we apply now selection sort for sorting singly linked list. Now this is an approach for sorting singly linked list using selection sort. Compare data of first node with data present at each node. If data of first node is greater than data of remaining nodes then swapping is done. So after one iteration first node is getting sorted. Now continue the same for second node to last node and will continue this procedure till all the nodes of singly linked list getting sorted. So this is a selection sort approach we are using for sorting of singly linked list. So this is an example suppose we have singly linked list like this 40 is my first node and start is pointing to node 40, 40 is next pointer is pointing to node 20, 20's next pointer is pointing to node 50, 50's next pointer is pointing to node 10, 10's next pointer is pointing to node 30, 30 is my last nodes. So last nodes next pointer will store null indicating that this is the end of singly linked list. So I am using two pointers i and j for traversing purpose where i and j data type will be a pointer structure node pointer and i will be my outer loop and j will be my inner loop. So I will compare value of i with value of all j's j will traverse from 20 to 30 or last node and swapping is done if value present at i is greater than value present of at j. Now first comparison is done 40 is compared with 20 now 40 is greater than 20. So swapping is done so 20 will now first node and 40 will now second node j will move forward again 20 will be compared with 50, 20 is not greater than 50. So swapping is not done j will move forward again 20 is compared with 10 not 20 is greater than 10. So swapping is done so 10 will be now first node and 20 will become now j's node. Now j will move forward again 10 is compared with 30 now 10 is not greater than 30. So swapping is not done so j will move forward now j will move to null now. So this is my iteration one so after iteration one my first node will getting sorted now. So this is my iteration one so after one iteration first node that is 10 is getting sorted. Now i will move forward now i will point to 40 nodes and j will point now 50, 20 and 30 like this till j will reach to null not 40 is compared with 50 again 40 is not greater than 50 swapping is not done so j will move forward 40 is compared with 20 40 is greater than 20 so swapping is done and j will move forward 20 is compared with 30 now 20 is not greater than 30 so swapping is not done and j will move forward and j will reach to null now. So after iteration two these two nodes will getting sorted and again i will move forward like this wise this process will continue till all the nodes of singly linked list getting sorted. So after five iteration five nodes are getting sorted you can pause the video now and you can answer what will be the maximum number of comparisons may happen for sorting the singly linked list with n nodes ok answer to this is if singly linked list contains n node in selection sort each node is compared with remaining all the nodes so maximum n square nodes comparisons are required which is nothing but a complexity of selection sort. Now we will see the program for sorting singly linked list now ok this is online compiler we have written i have written a code for creation of singly linked list and display of singly linked list so there are two functions i have used first one is display display function will print singly linked list from start to end and insert at end this is another function i have written for inserting a new node at the end for creation of singly linked list now i will write a code for sorting singly linked list so i will add another case case 3 so second this case 3 will be my sort function and this loops which do i loop will end when case is 4 here i will write sort function this is void sort it is not accepting any parameter and not returning any parameter now two pointers variables i am using here i and j this is this is struct node star i and j i will use another variable called a stamp for swapping of integer data present in two nodes now i will write a for loop for i equal to start i arrow next this is not equal to null and this instead of i plus plus i will write i equal to i arrow next now here i and j are not integer variables these are pointer to structure j equal to i arrow next j not equal to null j plus plus now here i am comparing if that is greater than j arrow data and swapping is done using temporary variable temp equal to i arrow data arrow data equal to j arrow data and j equal to this is standard code for actually selection sort same selection sort algorithm i am using now i starts from first node so first node i will get from start i will reach to last but one node so i arrow next is not equal to null and i instead of i plus plus for moving forward i i am using i equal to i arrow next so using this i will move to one by one node next now j will start from i's next node so initially j equal to i arrow next so j will point to a next node of i j will traverse to end so that's why the condition is j is not equal to null and instead of j plus plus it should be j equal to j arrow next so j will move forward and this is if condition if i arrow data is greater than j arrow data then swapping is done using temporary variable so temp equal to i arrow data i arrow data equal to j arrow data and j arrow data equal to temp this is swapping now I will execute this and I am checking whether this code is working correctly or not just a second I will create first suppose this is 20 this is 10 now this is my original link list which contains three nodes 20, 10 and 30 now I will call function sort for case 3 so after sorting I am displaying now case 2 so after sorting the link list will now 10, 20 and 30 so using the sort function I have sorted a singly link list using selection sort algorithm now I will again move back to a presentation so these are some of the references I have used for creation of this video thank you