Back to: Data Structures and Algorithms Tutorials

**How to Check if a Linked List is Sorted in C Language:**

In this article, I am going to discuss **How to Check if a Linked List is Sorted in C** Programming Language with Examples. Please read our previous article, where we discussed **How to Delete a Node from a Linked List** using C Language with Examples.

**Check if a Linked List is Sorted:**

Here we will look at the procedure for finding whether a given linked list is sorted or not. In the below example, we have a linked list that is already sorted. All the elements are sorted and we have to check whether the linked list is sorted or not.

Let us look at the procedure. The procedure is we should start from the first node. There is nothing previous to the first node, so letâ€™s move to the next node. Now the current node value should be greater than the previous one. Here â€˜3â€™ is greater than â€˜2â€™. Then move to the next node which is â€˜5â€™. â€˜5â€™ is greater than the previous node which is â€˜3â€™. So, in this way, we will check whether a linked list is sorted or not. The above LinkedList is already sorted. Suppose we have one unsorted linked list as follows:

Letâ€™s iterate again. We should start with the first node which is node â€˜2â€™. As there is no node previous to node â€˜2â€™ so move to the next node. Node â€˜5â€™ is greater than node â€˜2â€™ then move to the next node. Now node â€˜3â€™ is not greater than node â€˜5â€™. So, this is not a sorted list. So just check the condition that we are getting any value that is smaller than the previous value, if so, it is not sorted. If not, you have reached the end of the list you have checked all the nodes then the list will be sorted list. So let us see a procedure for this one.

We need the value of the current node and the value of the previous node. So, we will take a variable let’s say â€˜xâ€™, and let it be our minimum integer i.e. -32768. Now take a pointer â€˜pâ€™ on the first node. Then check if the data of the first node is greater than â€˜xâ€™. So â€˜2â€™ is greater than â€˜xâ€™ or -32768. So, modify â€˜xâ€™ to â€˜2â€™. Then move â€˜pâ€™ to the next node.

Here â€˜pâ€™ is on the â€˜5â€™ node. Again check, if (x < p->data), yes, so change â€˜xâ€™ to â€˜5â€™. Then move â€˜pâ€™ to the next node.

Now, â€˜pâ€™ is pointing on the â€˜3â€™ node. So, check if (x < p->data), no. So here we got a value that is not in increasing order. Here we have to stop and say the linked list is not sorted. Suppose all the values of the above LinkedList are sorted then the â€˜pâ€™ pointer will trace the whole linked list until â€˜pâ€™ points to null and return true as the linked list is sorted. Now let us write pseudocode.

**Check if a Linked List is Sorted Pseudo Code:**

**int x = -32768;**

**Node *p = first;**

**while(p != NULL){**

**Â Â Â Â if(p->data < x)**

**Â Â Â Â Â Â return false;**

**Â Â Â x = p->data;**

**Â Â Â p = p->next;**

**}**

**return true;**

In the above code, we are checking for failure conditions in if-block that is if pâ€™s data is smaller than â€˜xâ€™ then the list is not sorted. Let us trace a linked list with this code.

**Example-**

**Step1- **

First, the while loop condition will check. Here â€˜pâ€™ is not null then while loop will execute. Next â€˜if(p->data < x)â€™, no so statements of â€˜ifâ€™ block will not execute. Then â€˜x=p->dataâ€™ so â€˜xâ€™ is â€˜4â€™ now and â€˜p=p->nextâ€™.

**Step2- **

Here again, the while loop condition will check. â€˜pâ€™ is not pointing on null so it will execute statements inside the while loop. Next â€˜if(p->data < x)â€™, no so â€˜ifâ€™ block will not execute. Then â€˜x=p->dataâ€™ so â€˜xâ€™ is â€˜5â€™ now and â€˜p=p->nextâ€™.

**Step3-**

The same steps will be followed here. Here the value of â€˜xâ€™ is â€˜7â€™ and â€˜pâ€™ will point to the next node.

**Step4-**

Hereafter entering the while loop, the condition for the â€˜ifâ€™ block will be checked that is â€˜if (p->data > x)â€™, this will be true as â€˜3 < 7â€™ so it will return false that is linked list is not sorted. Now let us look at the complete program for checking whether the linked list is sorted or not.

**Program for Checking if a Linked List is Sorted using C Language:**

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; } *first = NULL; void Create(int A[], int n) { int i; struct Node *t, *last; first = (struct Node *) malloc(sizeof(struct Node)); first->data = A[0]; first->next = NULL; last = first; for (i = 1; i < n; i++) { t = (struct Node *) malloc(sizeof(struct Node)); t->data = A[i]; t->next = NULL; last->next = t; last = t; } } void Display(struct Node *p) { while (p != NULL) { printf ("%d ", p->data); p = p->next; } } int IsSorted(struct Node *p) { int x = -65536; while (p != NULL) { if (p->data < x) return 0; x = p->data; p = p->next; } return 1; } int main() { int A[] = { 4, 5, 7, 11, 12 }; Create(A, 5); Display (first); if(IsSorted(first)) printf ("\nLinked List is Sorted"); else printf ("\nLinked List is not Sorted"); return 0; }

**Output:**

In the next article, I am going to discuss **How to Remove Duplicates from Linked List in C** Language with Examples. Here, in this article, I try to explain **How to Check if a Linked List is Sorted in C Language** with Examples and I hope you enjoy this Check if a Linked List is Sorted in C Language with Examples article.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.