 Hello, learners. Welcome to this video lecture. And in this video lecture, we are going to see how to use Q sort function, which is an inbuilt function to sort elements using Q sort algorithm. So basically, at the end of this session, you will be able to use Q sort function to sort data that might be elements in the form of characters or strings or random numbers. And we will be able to sort this data, according to the order. So if you see this function Q sort, it is defined and is readily available in the header function sdlib.h. What this function does is it performs sorting according to Q quick sort algorithm. So any learner who is watching this video, I expect you as a prerequisite to go through the working of the quick sort algorithm. So you know how quick sort actually works. And this function will give you the exact functionality of the quick sort algorithm. And this function is already defined and available in sdlib.h header file. One more additional functionality of this function is that it makes the changes into the same array and then arranges it into ascending order. Now this is the syntax of this function. It takes four parameters. The first is a pointer to the array that we want to sort. The second is the number. The third is the width. And the fourth is a routine compare. Now I will explain the parameters one by one. The first is what we call your base is the start of the target array. The target array is the array that we want to sort or the elements in the array that we need to sort. So first is we pass the array that we need to sort. The second parameter that we need to pass to this function is the number. It simply is the size of the array that we need to sort. Say for example, usually we take this size as integer n and we store the size of the array into a variable and call it say for example n. So this parameter is nothing but the size of the array that we need to sort. The third is the element size in bytes. You can find out the size of the element that are in the array that you need to sort. You can do is do this by using the size of operator where you pass the parameter to the size of operator as integer or float whatever type of the data that is in the array. And the fourth parameter here we see is a function and it is a pointer to a function to compare two values. Now we are going to use two pointers here to compare two values in the array. Now let us see the fourth parameter in detail. It takes two values p1 and p2 where these two pointers are pointing to two values in the array. Now we know that when we are doing sorting we need to compare two values and these two values will have the behavior or will work according to the behavior given into the table. Now these two values here p1 and p2 will be either equal or p1 will be less than p2 or p1 will be greater than p2. Let's say for example p1 is less than p2 where p1 is 5 and p2 is 10. In this case the difference will give us a result which is less than 0. It means that p1 is less than p2 and it says that since the value of p1 is less than p2 p1 should be in a position which is to the left side or earlier than p2. If p1 is equal to p2 the difference will be 0 and it says that p1 and p2 are in its correct position when compared to one another. If the difference between p1 and p2 is greater than 0 that means say for example p1 holds the value 10 and p2 holds the value 5 and when the difference is taken in the difference is positive that means p1 is greater than p2 and p1 should be at the right side of p2. Say for example p1 holds the value 10 and p2 holds the value 5. So 10 minus 5 will give us a positive integer 5 which will give us the idea that 10 should be after 5 in the correct sorted order. So we define the routine compare as given below we need two pointers which will point to two different elements in the array and we just need to return the difference between these two pointers. What we will do is we will convert it to integer and then return the integer value as a parameter to our Qsort function. Now let us see the demonstration of Qsort function. Now this is the program that I have written down already which will take the input of an array of size n and we will sort it using Qsort function. I have already returned the compare subroutine here which takes two pointers as parameters and then we return the difference between the pointers type casting it to int. You can also define the subroutine as given from line 11 to 16. Both the subroutines will work in the similar manner. Later in the main function what I do is I take a integer n and also an array of size 10 of type integer. I scan the value of n which gives me the size of the array that I need to take the input and similarly I take n values as input and store them in the array arr. I simply call the Qsort function by passing the four parameters that is the array to be sorted the size of the array and the size of operator just to let the Qsort function know that integer values are passed and the compare subroutine and lastly I print the sorted array. Now as given in the description of the function the Qsort function will change the position in the self-same array. We need not to create a separate array to store the result. Let us see the working of this program. Now let us take an array say for example of size 5 and some random values. I hit enter and as you can see in the result the array and the elements of the array are in the sorted order. This is how Qsort function works. Now at this point in time I want you to pause the video and I want you to ponder on this question that how to sort the data in non-increasing or simply said decreasing order using the Qsort function that we have just seen. Pause the video and answer the question. If you have carefully studied the compare subroutine you will understand that you need to edit the compare routine so that it returns positive value if the first number is less than the second and returns negative value if the second number is less than the first number. You need to make the changes in the relational operator and when you make the subsequent changes you will be able to sort the data in the non-increasing order. So these are the references you may refer to these references for further information on Qsort function. I have also added the program that we have seen in today's video lecture in the description section. Thank you.