 Hello and welcome back. In this lecture, we are going to look at the sorting problem and in particular we are going to look at some motivating examples that hopefully will allow us to appreciate the importance of sorting as a computational problem. Here is a quick recap of some of the topics we have already studied. We have basically studied several constructs to write useful programs. These include assignment statements, arithmetic and logical expressions, basic input and output, sequential and conditional statements, looping constructs or iteration constructs, functions and the use of arrays and matrices. In this lecture, we are going to look at the sorting problem as a computational problem and we are going to look at specifically some motivating examples and see how sorting actually serves a useful purpose in several different contexts. So here is a simple example. Let us say in a table or in a spreadsheet, I have listed down the quiz 1, quiz 2 and quiz 3 marks of several students who are taking CS 101. So each of these entries represents marks of a particular student in a particular quiz and then we have added up all of these marks to get the total mark. Now given this table of marks, one might ask can we rank the students who are identified by their roll numbers here in decreasing order of total marks or we could ask find the n top scoring students in quiz 1 where this n could be user specified or we could ask find the n least scoring students in quiz 3 where this n could again be specified by the user. Now if I look at the first problem rank all students in decreasing order of total marks. So one way I could do this is to inspect this total marks column of all the students and perhaps come up with some annotation saying this is the highest marks. So in decreasing order this would appear first then this is the next highest mark. So in decreasing order this would appear next and then we have two students who got 24. So in decreasing order both of them would be at position 3 and then we have 18 and 17. So I could look at the rows of this table and come up with these annotations on these rows and then by finding out which of the row has the annotation 1, I could find out the highest scoring student which of the rows has annotation 2, I could find out the next highest scoring student and so on. Wouldn't it be convenient if in order to answer this question I had these rows already sorted or ranked by this order. So basically the row which has the highest marks in it is labeled 1 here. If this row was at the top of this table and then the row which has the next highest marks total marks in it here labeled 2 if this was the second row in it and so on then it would be very easy to read off the ranking of students in decreasing order of total marks. So that is exactly what I have done in this slide I have just rearranged the rows. Now note that when I am rearranging a row when I am picking a row from here and putting it in the first position I have to pick the entire row. So the quiz 1 marks, quiz 2 marks, quiz 3 marks, roll number and total marks all of it comes together in the first position then all of it comes together in the second position and so on. So this is how the ranked order of students ranked by total marks might look like. Note that once I have ranked the students by total marks it is not necessarily the case that they have been ranked by quiz 3 marks or it is not necessarily the case that they have been ranked by quiz 1 marks. For example here this appears before this so it is certainly not ranked in decreasing order of quiz 3 mark. Now if you look at these two entries in between both of them had the same total marks 24. So if I am going to rank them I have to decide who appears first and then who appears next. So which entry appears topmost among equals and here I could decide that well among these two entries which have the same total marks I could decide to put the one with the lower roll number first and then the one with the next higher roll number. So this could be one criterion and if I use this criterion I will get this ranked ordering of students with their total marks. If I am trying to find the n topscoring students in quiz 1 this is what I might get once again there are three students who got the same marks in quiz 1. So they are all tied at the second position how should I list them how should I order them. So one could say that well among these students ranked them in decreasing order of total marks and similarly I could say find the n least scoring students in quiz 3 note here that unlike the previous two examples I am interested in the least scoring students. So I should probably order them by putting the least scoring student first then the next higher scoring student and so on. So in this particular case the first row would be the student who has scored the least number of marks in quiz 3 then the student who has scored the next higher marks in quiz 3 and so on. And once again here we have two students who have scored the same in quiz 3 and we could order them by total marks for example. So if you think about this example that we just looked at here are some observations we had the same data which is basically marks of different students in quiz 1, quiz 2, quiz 3 and the total of these three quizzes marks. But the different questions that we asked required us to order them by different criteria. So in general we might be given a collection of data items in the previous example each row of the table of marks is a data item and we might be told how to order these data items. So here I have described it abstractly by saying that we are given an ordering operator such that for every pair of data items di and dj either di is ordered with respect to dj in this way or dj is ordered with respect to di in this way. So this is also called a total ordering and what we are saying is that given a collection of data items and given an ordering operator which actually gives for every pair of data items which is ordered before what we are now required to rank or sort these data items according to this order. Now as an example of this ordering operator in the previous quiz marks example one of the ordering operators we saw was increasing order with respect to quiz 3 marks. Remember we wanted the lowest quiz 3 marks first and then the next higher quiz 3 marks. So this order was increasing order for quiz 3 marks and if two quiz 3 marks were equal if two students had the same quiz 3 marks then we wanted to order them in increasing order on total marks. So that defines a particular order and for every pair of data items we can order the two students in this manner. Similarly another ordering that we have looked at in the previous example was when we said we wanted decreasing order on total marks. So here the ordering was greater than equal to on total marks and if two total marks happened to be the same then we wanted increasing order on roll number. So what you see is that this ordering operator or the comparison operator could be some complex operator like this. However all that we require is that for every pair of data items either DI for every pair of data items DI and DJ either DI should be ordered with respect to DJ in this manner or DJ should be ordered with respect to DI in this manner. So it should be the case that for every pair of data items I can figure out which appears earlier in the order and which appears later in the order and given the data items and given this ordering operator our problem is to order the data items such that once we have ordered them they monotonically increase with respect to this ordering operator. So note that I have said increase with respect to this ordering operator but this ordering operator is not necessarily the less than or equal to operator that we usually know of we have just seen that this could be a much more complex operator. For example if we wanted to order the data items such that the monotonically decrease I could define this ordering operator to be the greater than equal to and this is what is called the sorting problem in computer science. Now having seen what the sorting problem is primarily about let us look at another example that several of us might be familiar with. Suppose I want to purchase an airline ticket online let us say I want to go from Mumbai to New Delhi on December 25th 2014 and I want to plan early and purchase an airline ticket online right now. Now as several of you know there are several online airline ticketing portals you could go to any one of them and then it will list the various options the various airlines tickets that are available for December 25th 2014 from Mumbai to New Delhi and all of these options will be sorted by various criteria for example they could be sorted by time of departure they could be sorted by price they could be sorted by number of stops they could be sorted by duration of travel and usually the user has the option of deciding what the sorting criterion should be and the user also has the option of saying whether it should be sorted in increasing or decreasing order. For example I might be interested in finding out the earliest flight on December 25th 2014 from Mumbai to New Delhi or I might be interested in the least expensive flight for the same source and destination on the same date. So basically the user has the option to decide what the sorting criterion is and the user is usually interested in the top few options in the top few ranked results for a given criterion for a given ordering since these are likely to be the most interesting. I might be interested in the cheapest ticket or the earliest leaving flight and of course the sorting criterion could vary from one user to the other what is more useful for one maybe less useful for another. So the same data can be sorted by different criterion to come up with different rankings different orderings. Here is yet another example considered a telephone directory a telephone directory usually consists of a name and telephone number pair for different individuals. Now suppose I listed out this telephone directory in a completely arbitrary order. So here are some names here are their numbers and I have listed them out in some completely arbitrary order and then let us say ask you to find the telephone numbers of Shiva Iyer or Anjali Verma from this telephone directory. How would you go ahead and do that because these numbers are listed in completely arbitrary order what you would really have to do is you will have to do a brute force sequential search you will have to look at one name and then the next name and then the next name until you find Shiva Iyer or Anjali Verma. Now this is very inefficient computationally and one could ask what would happen if the names were sorted alphabetically as in a dictionary by first name and then by last name. So Alex would appear in a dictionary before Anjali because L appears before N and both of them start with a. Similarly Anjali Singh would appear before Anjali Verma because the first names are the same but the first letter of the last name here appears before the first letter of the last name here and then of course Shiva Iyer and Subodh Das might appear and of course there could be several other names in between. Now if I did sort the names alphabetically as in a dictionary then if I asked you to find out the phone number of Anjali Singh or Shiva Iyer or Anjali Verma whatever it is it is much easier to find a given name we can first go alphabetically on the first names and then among different entries with the same first name we could go alphabetically on the second name or the last name. So what you see is that if I could sort the data on some appropriate criterion here it is alphabetically first on first name and then by last name then actually searching becomes easier and in fact this is a very important point that we will revisit again later. So in summary in this lecture we looked at sorting as a natural computational task we looked at a few real life contexts in which sorting is applied and we also had a brief glimpse of how sorting can help searching and in fact sorting and searching among the two most fundamental problems in computing and we will have occasion to study these in more detail in the next few lectures. Thank you.