So, in the last class, we were discussing one of the interesting algorithms that is merge sort. That is primarily based upon the divide and conquer strategy. We have already concluded the asymptotic analysis. Okay. Then we started discussion on the divide and conquer in last to last lecture. And then we understand that the divide and conquer algorithm was basically multiple division. and division will cut a recursive division not multiple division if I can say a recursive division approach where a large problem is divided into multiple sub problems and this division will continue until we have reached to a situation where the array is having one one element. After doing this we will do the conquer operation. In conquer operation what we will do? We will try to solve all those sub-problems and combine means that we will combine those sub-problems solutions to form the final problem. So, I hope you people have got a visualization how this merge sort actually works. So, here we can either go for n by 2 int, n by 2 float or n by 2 ceiling values. Okay, so that is the partitioning criteria and if the number of element is less than or equal to 1 in an array, then that is the base condition of the recurrence relation and where we have to make a stop. Okay, so what I can say, merge sort follows recurrence relation, merge sort follows recurrence relation, okay, to divide the problem into sub-problems until we have reached to a situation of single element. Okay, so you can say the f n for merge sort will be n into f of n by 2 plus whatever the division term and there will be 1. So, or we can say this is not the thing. fn is equal to n by 2 or if n greater than or equal to 2 if it is n equal to 1. So, here we will stop. When the size of array is 1, then we will stop. So, this could be the recurrence relation. So, in recursion also, after some time we will start recursion. In the recursion, there is a two important thing. one is the recurring condition maybe this is not the exact thing but i'm just giving you the example recurring case and this is called as the base case okay base case so what is our ultimate in to recur again and again again and again again and again to reach the base condition to reach the base condition so here we divide we keep on dividing we keep on dividing unless we have reached to one one element label so that is called as the base condition when we are in a situation that we have reached to single element label okay so then we understand about the these things okay that how we can do a merger of two cell arrays okay that is important operation because in division nothing you have to do just n by 2 n by 4 n by 8 okay and so on till we have reached to single single element label but but the important operation or that imposes a computational complexity of order of n because of if you see 1 2 3 4 5 6 7 8 so maximum comparison that could be performed will be 7 so that could be order of n okay and this part this merge part so in merge sort if you see the algo there are two component one is the recursive merge call this is called as the recursive recursive merge call so here if you see if length is less than or equal to 1 then we are returning the array because the array size will become size of ARR will be equal to 1 otherwise what we are doing middle equal to length of array by 2 left half right half left half right half okay and when both left half and right half have one one entry then the algorithm will stop and we will call the merge operation okay now what this merge operation will do it is initially declaring an empty list okay then initializing two indexes left index and right index as zero left indexes right index as zero okay then it is checking while left index is less than length of left so length of left it's a one-one element so of course first time it will be true then right index should be less than right of less then what we are doing seeing the first element of left is less than first element of right if it is true then result dot append that value and increase the index otherwise what we will do result dot append the right build so whichever is smaller either left or right will be added into the resultant array s okay and Now, what happened? Why these two lines are written? Because let us say during comparison, all the elements, if all the elements of one subarray is reduced to 0, means that there is no element or we can say that or the subarray is empty. okay this means that there are still some element in the left so very let's say 21 25 but right so where is empty in that case what will happen this loop will fail and we will just extend the either left part or right part whatever is remaining so let's say result of some entries we'll just append the array that is having some element if another array is having zero element okay so this means that at any instance during comparison if all the entries of left savary or right savary are removed or all the or the size of array becomes zero in that case this while loop operation will be terminated and last two step will be called so we will merge right savary and left savary whichever having element will be extended directly okay because we know that we cannot we know that the sub array that we are doing going for merge is already sorted and if there is no element remaining in the left sub array this means that we can simply merge with the right one okay so that is an important consideration given here in this question then i have tried to give you some glimpse of realistic examples so here we have two severity left and right that is having one three four five six so what i have declared result as an empty list left index equal to right index initialization with zero and while we are checking while left index is less than length of left severity so if you see left severity length is how much three and right index is less than three as well so what we will do check if left zero is less than right zero first index we are comparing what we found yes it is so in the result append one so append one means that this is a resultant array left index now become plus one and right index become zero now cursor of left has shifted forward but right is still there so now two and three will be compared so what it is saying append to right index will be increased by one okay now then three and four will be compared whichever is highest so three is four is highest three is less so append three so resultant is one two three left index is two right index is one then four will be compared with five so append four append four okay and right index will be incremented by one then five is less than six so this value we are getting and what what we will get this the while loop that is running here if you see in this code this while loop will fail because to to ensure while is correct the left index should be less than three and there is an symbol means that both the situation should be correct so one one should be there okay both should be correct in that manner this file loop will fail and the extend operation because if you see here left extension no element but right extension there is element okay so in this way we will get the resultant um array by doing the merge operation so this is nothing but this is merge operation in merge sort okay similarly we have another a few examples okay so already we have witnessed this then now if you talk about this one one two seven three four five six okay so that is going similarly if we have no element in the left sub index and some element in right sub index the while loop will fail and finally just right index will be appended okay and so on so I hope you people got the idea if any of the index is failing this situation that is less than right index or left index then this while loop will automatically fail and we have to just extend the either the left portion remaining element or the right portion remaining element in the result. So, result was a list and in the previous discussion I have said that yes was the array on which we are adding the elements. If we talk about the asymptotic analysis that we will do after recurrence relation because here we are talking about the recurrence relation. So, what we see, if we have a problem of size n, so basically we are dividing into problem of 2n by 2, because we are using what? Simple n by 2 int, okay. And plus order of n time is required in sorting the elements in ascending order, because we are doing comparison with this this this this and so on so maximum comparison will be seven okay so what we can say that if your problem is t of n so we can deduce the problem into 2t n by 2 and it's merger required order of n and it's merge required order of is much different order of okay so here if you see dividing the array dividing the array will take order of one because we are not doing any comparison we are just divided men come just calculate n by 2 go ahead and create n by 2 go ahead then conquer the recursive call on the sub problem occur until it's a problem contain only one element result into order of login label of recursions this recursion will run division division division division and so on and merge will obviously take order of n times so the recurrence relation for the merge sort is tn equal to 2t n by 2 plus order of n okay so later on we will see how this recurrence relation is converted into n log n complexity okay but at as layman or at this stage what we can say that conquer is taking log of n and merge is taking order of n and all the steps are executed simultaneously so this will be the multiplicative property multiplicative property of merge sort in that manner n log of n will be the computational complexity n log of n will be the computational complexity thing I hope you people get the idea of much sort merger so that's why I have started this and we have already started discussion on a binary search and then we will start quick sort today let me close this and open binary search the basic idea is so much sort basic idea was if you think a big area is given then I have divided into a small small areas small and further small and further small till one element label okay at one element level the basic idea in so when we reach to one element level then what we were doing we were doing the merge operation then combining these then going up then combining these then going up and so on now finally such basic idea is if a bigger is given okay bigger is given important consideration that it hold that is the assumption this assumption is quite critical the assumption is all the elements elements are already sorted that is important and you can see drawback or that is quite important already sorted the element in this server in this area is already sorted that is an assumption now I ask the thing searcher element in this area let me add one okay so I have a big array up straight then that is already sorted and And the question is, identify an element of interest. That is the key. Now we know that there is one important thing is that this array is already sorted. This means that if we follow the normal linear search strategy, so what we will do, we will compare the key with this, this, this, this, this, this, this. and if I found the key here then I declare that the result is present present but what binary search did binary search say I will go directly to the middle that is n by 2 then check whether I should go this side or this side depending upon the value of the key so let's say I go on the left so I will left middle and now if the 0 5 and 10 it is so we have only 0 to 4 element because one comparison we have already made with the middle then again we will do a middle then either this side this side then if I said there is a light right portion then again we will do a middle and let's say in log of n comparisons we will reach to our solution or are in a state we can declare that the element is not there okay so this will save time because every time we are reducing our search space by half so the basic idea is every time we are reducing our search space by half okay so every time your search space is reduced by 2 that is 1 by 2 1 by 2 1 by 2 and so on so same thing is there so let's say we have these set of element okay and l0 or this is low value lower index okay and this is higher index okay now the numbers are 6 again 6 13 14 25 33 43 so what we are seeing that this these are already sorted in ascending order okay now our goal is to identify where 33 is located okay so in place of searching one by one element what we will do we will first reach to the middle element so 14 14 by 2 will be what 7 so 0 to 7 and then we will compare 53 with 33 so what we will find 53 is greater than 33 so the chances of getting the element will be toward the left only if you go in the right there is no further chance then what we will do we have an hour survey of size 0 1 2 3 4 5 6 ok then now we have to search 33 in these six element and this portion is now blocked this portion is now blocked so again we will calculate the middle compare 33 what we want 25 and 25 says they if 33 you want to identify you to go on my right there is no chances to get the element on my left so what we have did from the size of problem 14 we have reduced to 6 now we are reducing to 3 then we will reduce to even 2 also okay so here then middle element is 43 43 when compared with 33 say you should lie in my left so there will be one element that is low as well as high index as well as the middle and that is our resultant solution and hence we get declared so what we found doing only three comparisons that is comparison with 53 then comparison with 25 then comparison with 43 and last comparison with 33 that is for comparison can help in identifying the the our key element or let's say if it is not found then we can declare that particular element is not present okay the element is not present so if you see there are 15 elements 0 to 14 so log of to 15 so it will be around if you can say three point something or if you go with the ceiling so it will be four so maximum four number of comparisons we are doing so what we can say that the recurrence relation for this one will be T n and And every time we are reducing the search space by 2 plus doing one comparison. So, this is the recurrence relation for the binary search. Okay. So, let us consider another example. Just give me. This is another example. Here we want to search for 20. Okay. Now, number of, if I just need to start the index, that is 0, 1, 2, 3, 4, 5, 6. So, number of index is 6. So, if I do n by 2, then where I will reach? I will reach 3. So, here we have reached. And key that we have to search is nothing but 20. We compare 20 with 100. What it say? You surely lie in my left. So, this, so now we have 10, 20, 50. The indexes are 0, 1, 2. So, 2 by 2, that will be 1. Okay. Just give me a moment. Yeah. Now, if I compare 20 with 20, you say, okay, yes, I got it. And here we get the result. So, what we can see that when we go log of 2, 7, so it will be primarily maximum 3, but here in 2 comparison only we have obtained our result, okay. So, this is the beauty of insertion sort that is maximum computational complexity can be log of 10, okay. So, this is the primary strategy. If k is equivalent to the a n by 2, we are done. Otherwise, we will search either left or either right. If k is less than n by 2, that is if k is less than middle, if k is less than middle, then we have to search on the left. if this condition next condition if key is less than middle then we will search right okay this is an important consideration that we have to do this is simple implementation of the binary search so that you can just do a copy paste in see implementation and we can go okay so now i will take some queries and then we will start this quick shot okay so yes yeah morning sir morning sir i have one question when you were teaching binary search now in the morning you started with creating it with merge sort sir merge sort is a different algorithm it is basically merging and sorting the elements and then putting them into an ascending or descending order how we design it. But binary search is actually running a search on the sorted array. So, is it correct to say that firstly both are purpose of both the algorithm is separate and secondly merge sort is kind of a prerequisite before a binary search can be done. That definitely we can say. But why I am correlating both of them because they both follow divide and cong. So, their their target is to divide the problem into smaller problem or divide the search space into smaller search space so that's why i was comparing but yes of course merge sort is prerequisite or any of the sorting is prerequisite for binary search thank you good morning sir good morning sir just i want to know in our real application whenever we are doing sorting searching correct we are just writing that sort method and it is doing the sorting correct whether it is python whether it is java so on the hands are they are not doing selection whether we use the insert and sort merge up quick sort how it is because uh in those all those algorithms are different different complexity so how that decision our miss language is taking on just i'm thinking that we are doing the sorting where we do this real implementation there in the means actual project we are directly doing the sorting which is setting a search method all those things we are range okay so after some time we will cover one of the interesting trees that is b3 and b plus tree okay so they are the how the files are arranged or how indexes are maintaining the database so on each node okay so there are different nodes we have some sort of element and every iteration every insertion and deletion on that specific node required a rearranging or sorting so that is the you can say biggest application of this sorting and searching stuff where in a large database where number of indexes are maintained in a hierarchical order if we want to search a specific key we have to apply this searching technique and if we want to add new tools we have to go for an arrangement that required an algorithm like this one of course insertion sort will not be used because we know that records are millions in order but if very smaller size or few elements we have to search so we will go for the insertion sort so decision of specifically applying to a realistic application because this binary search is quite famous for b3 and b plus three for sorting the element on each node okay so and we know that because b and b plus three is you can say a variant of binary search tree there these things are applied that i will explain in detail so just understand because to understand the concept of divide and conquer these algorithms are working but in real scenario if you want to see you have to see the databases how b3 b plus tree fibonacci heap and other things are used okay so sir actually in java suppose i'm doing the sort dot sort method i'm calling there so by how it is selecting this sort i can take insert no no so by default by default there is some set of function that is already being installed if you search on the uh google or any ai tool so you will find what sorting it is actually using so it will never select merge this that so there is a specific sort that is fixed to at its end or if you want to make a function like this so let's say I use insertion merge this quick sort I have put all three okay then what I will do I will see value of n if it is let's say 20 then I will call insertion sort if it is hundred then I will call my sort and next thing I can take if there is available space yes or no so yes then I will go for much sort if no available space then i will go for quick sort if the area elements are fairly sorted then i will not prefer quick sort so that kind of condition you have to write so just explore it keep what actually it is quality but in my knowledge there will be a specific algorithm that is running on behalf of every sub problem we are supplying okay okay yeah i will check that one thanks okay i hope this is not a tough thing and we have already discussed so this is easy to go so let me start one of the interesting algorithms that is quick so it's as its name is indicated quick sort so this is also known as one of the fastest sorting algorithm though the computational complexity that you see it is n log n and for merge sort it is also n log n and further we will see heap sort that is also having n log n but this is fastest because one thing that we already know that it does not uses any external space and second thing is that the pivot selections readily reduce down the computational requirement in some some aspect not in very wide aspect later on we will also see why this algorithm is faster than much okay so I'm just taking a two minute break then I will come back and start this quick sort okay and those who have query can again ask when i will provide the time yes let us start so sorrow i guess you had to worry i'm enabling your mic yes sorry good morning sir good morning basically i have a question uh the one that we have done before like the insertion short and the search okay iterative search and that one basically uh that comes under brute force right yes yes yes insertion come under brute force okay so basically that was brute force and now we are moving we have moved to divide and conquer and later on also we will see some algorithm via brute force some wire divide and conquer but the actually in some of the literature when you go so go then brute force is not assumed to be a algorithmic paradigm it is just a thing of what he whatever we want we can do or search everything but of course it is a paradigm but the main paradigms are this divide and conquer dynamic greedy and other but of course brute force is also there okay so uh we have started with the brute force now we are shifting toward divide and conquer later on we will also see the dynamic program okay sir yeah at this enable mic for these two three peoples yeah hi sir good morning ah good one yes yeah sir uh so far we have been planning uh covering the concepts of all these things right uh in real time how we are going to use uh these concepts when we are going to use can we uh can you give some context on this so one example already i have stated that is v3 or or storing element in the let's say i have stored the element in the memory okay and i want to identify something so that is also a search of addresses okay so that that could be one example and bb plus three are using databases uh where multi-level hashings are used and in that at each step we have to perform the search okay so primarily if you talk about the searching thing so it is basically used when we are retrieving the document from the secondary storage to the primary storage and sorting is needed whenever we have to make the arrangement or the placement of the element so that we can reach to our solution or the label as fast as we can okay so that is important because this searching sorting stuffs are not primary operation primary operation in any memory system is of uh reaching the records as soon as possible so the the that that is the you can say real world application you can witness yes okay thank you any other problem then i will go for divide and conquer uh enable because hello i'm out of it ah yes your order will be louder yeah yeah okay yeah good morning sir uh good for binary search uh the the initial criteria is that ira should should always be sorted sorted sorted always sorted that is you can say drawback of binary search so again like the time complexity and everything is always considered only for the searching part and searching not for sorting yeah yeah okay thanks good morning sir good morning sir you said that you will be uploading the assignment but it's not showing in the assignment part no no i have uploaded but the team will may may be delaying okay at their end okay now I have received the uploading for the assignment number two that I will do in a couple of days but I don't think why you people are not getting the assignment okay this I guess we have done with all the questions so we can proceed okay just give me a moment just give me a moment I'm Okay. So, there is some problem with my mouse. Just figuring it out. Okay. So, the next sorting algorithm that is again a divide and conquer strategy based approach. And it is quick sort. As name indicate, quick. So, it is quickest or fastest sorting algorithm that we can say. Okay. And it follow also the divide and conquer strategy and it is known as partition exchange algorithm, okay, partition exchange algorithm because here we will not directly do n by 2 or floor n by 2 or ceiling n by 2, we will do something else, we will do something else, okay, that we will discuss. So, in case of merge sort, what basically we were doing? We were going to the middle part, all which. And middle part was acting as a pivot for division of these two subparts. And in case of binary search, also we go to the middle. Then we are deciding whether we should go left or right. Okay. So, this means that this middle element act as the partitioning part or act as a pivot. okay act as a pivot for division so here in quick sort we will work differently and here our target is to freeze or fix the position of the pivot in each iteration okay so here what is our target to fix the location of selected pivot or we can say chosen pivot okay after each iteration for each iteration we will try to fix the pivot what is pivot is it could be a random element initially generally zero it's index element okay so simplest cases just assume the first element as the pivot element and after the iteration one it It will be reached to the specific location, okay, that is its sorted order. But choosing the right pivot is very important and affect the complexity of the algorithm. Later on, we will see there is a median of three mechanism that is quite popular in this quick sort, but the simplest way is to choose a pipe, okay, as first element, that's the pipe. So, here the thing is, pivot left should hold the element less than pivot and pivot right should hold the element greater than pivot. This is our strategy. If I choose a pivot and I am saying that I will freeze its position, artifact iteration, so all those elements toward the left of pivot should be less and all those elements toward the right will be more. okay so here if you see here if low is less than high low means that this lowest index and high means that the higher index then we are doing a partitioning of a into low and high so then we are calling a partitioning algorithm otherwise we will call quick sort so this is the partitioning algorithm so here we have supplied an array it's lower index and it's higher index so let's say zero and five so six elements are there okay now from these six element what I will do I will divide based upon the value of the pi board so initially I select first element as five wood and later on we will get two sub arrays for division and that if we can see quick sort a loop I vote minus one I would because its position is now fixed and fix what a pivot plus I so pivot is the element whose position will fix after one iteration and we will get to some area on which we have to again recursively apply the quick start okay now here if you see the simple algorithm okay so what I am doing pivot equal to a of loop loom is that 0 a 3 index okay in somewhere it is first index as well okay So, it is 0th or first index we are choosing as 5 volt. Then what I am doing, left equal to low plus 1. So, let us say here is an array with some element. Let us say this is 9, this is 1, this is 3, this is 17, this is 6, this is 4. And this index I have chosen as 5 volt. Lowest index, let us say 0, 1, 2, 3, 4, 5. Now, this immediate right index will be treated as left and this last index will be treated as right. And what is the goal of this algorithm? To place the pivot to its fixed position after each iteration. Then we will call quicksort on one sub half and another sub half. Okay. So, while left is less than right, what we will do? here we are we are checking whether the element is less than pi would if it is less than pi would then left will keep on increasing left will keep on if it is greater than pi would and what is our target left should be less than pi would then we will change the direction so we will start seeing the elements that are less than pi would if there is a chance that we get element higher than the pivot then we will change the direction and start witnessing the element whether the elements are greater than five would because on the right we should place the element greater than five so the simple strategies choose by would have chosen one five would then from left to right move until we are getting element less than five would so here you can see if a of left is less than five would then left will be incremented by one so we will keep on moving when the element is stored on the left index that is a of left index is less than pi would if at any chance we got an element that is greater than pi would then we will stop and change our direction and we will call this loop a of right greater than pi would then right will move toward left by decrementing its counter okay and if all the operations are happening then we will do a swap of a of left and a of right okay a of left and a of right will be swept again this above loop will be called and this operation will continue till last and last one will be the swapping of a of right and the pivot and after this we will find the fixed location of the pivot so let us see an example how this algorithm actually works but the simple strategy is choose a pivot and after pivot start moving toward the right until we have obtained any element less than the Bible if at any chance we got an element that is greater than pivot then we will change our direction and start looking for the element that are greater than pi volt and if both the conditions fail then what we will do we will do a swap of left and right entry and again the main while loop will be called and this process will continue until we have reached to a situation that we cannot do further comparison okay or until left low is less than or equal to high okay so see with an example here this is an example So, A of low is what? Pivot, this is the first element. The just next index will be left and last index will be right. Up till this, I hope there is no problem. Then what we have to see while left is less than right. So, if you see left values, so if I talk about the index 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. So, 1 is less than 9, of course, yes, true. So, we will start our journey from here. So, we will then start while A of low, A of low means that A of 0, A of 1 is less than pi volt. So, if you see, 12 is less than pi volt, condition fail. So, we will jump into the next while. A of right, that is A of 9 is greater than pi volt, condition fail. then we will check if left is less than right if left is less than right yes so what we will do we will do the swap of 12 and 6 so 6 comes here and 12 go there that is the thing okay now what will happen so if you see the example I have exchange 12 with 6 now again the bigger while loop is called so while one is less than nine and inside that there is another while loop a of low that is one because indexes are zero So 1, 2, 3, 4, 5, 6, 6, 7, 8 and 9. A of 1 less than pi volt. So if you see here 6 is less than 8. Yes. So left will be incremented by 1. So now this value become the left. And again I will check A of left that is 2 is less than pi volt. So 14 is less than 8. No condition failed. So now control will go here. Okay, so again while A of 9 greater than pi vote, so it is yes, 12 greater than 8, so right will be decremented by 1. And now right start pointing to this and left start pointing to this. Okay, again this while loop will call and we encountered 4 greater than 8, condition fail. Then what we will do, if low is less than high, so low is how much? 2 and high is how much? 8, condition is true. then 14 and 4 will be exchanged so in place of 14 4 will come and here 14 will come and this process will continue till we have reached to a solution limit so here if you can see well when 14 are swept the next step 4 and 14 are swept then in the next step 15 and 9 so condition is fulfilled because a of left 15 that is not less than 8 so we will go to this direction 9 greater than 8, 11 greater than 8, 10 greater than 8, 13 greater than 8, 15 greater than 8, 4 greater than 8 fails. So, this right will keep on moving because we have encountered the values that are greater than 8. So, it is moving, it is moving, comes here also and stops here. Okay. And if you see if left is less than right, fail because left is greater than right. So, main while loop will be terminated and A of right will be, A of right will be swept with the pivot. And after this what we will get? 4, 6, 8, that is the pivot, 15, 13, 10, 11, 9, 14 and 12. So, here if you see the position of 8, it is now fixed and we are on the correct position of placing the 8. is the final push again I'm showing the you the visible and with the visible I will also explain so I have chosen eight as five would first at the left index will be chosen one greater than five would that is well okay and its index is one and last index is nine and its value is six now what we said we are saying that if I choose a pi board so all the element to the left of the pi board should be less than Pivot and all the element greater than Pivot should be the right of Pivot. Okay. Now, if at any point we found these conditions are violating, then we will change the direction. So, first we will start with the left, compare the left first element with the Pivot, what we found that the condition is failing. Then we are shifting to the right, again condition is failing. And in the, this algorithm if you see, there will be a swap operation between left and right if left is less than right those are the index so 12 and 6 are exchanging we due to the if condition due to that if condition okay now these are swapping then we reach identified 14 that is greater than 8 condition fail of the first inner while loop Then we will shift to the next inner while loop. So, we found 4. So, both fails. Then if condition apply, 4 and 14 will be swept. So, again this swapping is performed by if condition. Then we found 15. The condition is failing. Then we have shifted to what right. The condition is true. 9, 11, 10, 13. Slowly, slowly the cursor will move. The right index will be reduced by 1. And at last we will stop when we encounter 4 that is not less than 5 volt. and at this point we also witnessed that left is greater than right so main while loop will terminate and at last there will be a swap between the a of right and the pivot and we have reached to our first iteration completion now what i will do i will take this array okay and if you see indexes so it is one two sorry zero one two its index is three four five six seven eight and nine now what what i am doing taking 15 as pi vote for this summary now 13 become the left and 12 become the right now a of 13 greater than less than 15 yes so left cursor will move ahead now 10 less than 15 yes left cursor will move at 11 less than 15 yes cursor move ahead 9 less than 15 cursor move ahead 14 less than 15 cursor move ahead 12 less than 15 cursor move ahead and stop because we have have reached to the final state and there will be no while loop execution because all the conditions will fail and now left is not less than equal to right so main while loop will terminate and what we will found 13 10 and 12 will be there because there will be swept between pivot and the first element 11 9 14 and 15 and this is now the fixed location of the 50 now we have remaining this 12 13 10 11 9 and 14 so what we will do we will again apply the quicksort and choose the 12 as the pivot then it will go to the fixed position so basically what it is happening the element that we are choosing as pi would it getting its its location after each attenuation simple strategy choose pi would choose left and right start moving from left toward right up till we obtain the condition that pivot is less than the b ski because we know that we have to do what pivot left should be less than pivot and pivot right should be greater than pivot if at any point the condition fails then we will start moving from right to left and this will continue when pivot is less than these entries because on the right we want later entries okay and if both the instructions are running then ultimately we will fix down the position of the pivot element okay and after fixing we have to reapply the quicksort on the left sub half and right sub half okay now one question arises if i choose a pivot okay if i choose a pivot that is not optimal or dividing likewise 15 is what 50 is extreme end so we are getting a left survey of more element and right survey of zero element okay so therefore the selection of the pivot really matters in quick sort okay so this visual is very much clear you can just visualize and see the algorithm it is it will work okay now i will take the queries because from the queries most of the doubts are clear then we will see talk about other things hello sir good morning good morning sir I have a question around this sorting algorithm so basically in here what we are doing we are fixing the position of pivot element and then we are partitioning the entire array into two half and based upon that pivot partitions yes yes and then the same iteration will go on for left part as well as right part after fixing the key element is the payment element right yes okay so basically uh i mean in here if we see and we do the asymptotic analysis so the time complexity is increasing right because we we have to perform the sorting for both the partitions and this iteration is keep on going for each of the partitions so what will be the final time complexity for this I will come if you go with this simple logic so here if you see if low is less than high then partition quick sort so this is order of one just one comparison we are making but the main computational complexity part is this stuff okay and if you see here so here this while loop will execute for n minus one time but either of this loop will run not all okay so so the total time taken will not never will be greater than n the number of comparison that you will perform because because it is not running for all the from one side to another side if it run then it of course go to order of n square and that is also said that the worst case computational complexity of quick shot is not n log and it is n square worst case will occur when we are choosing every time the extreme element that is either the smallest one or the largest one okay so therefore pivot selection is a challenge so in that case only we will reach to the n square otherwise we will reduce down the case to the lower as well okay and therefore for best and average case it is assumed to be n of log of n okay so that we will see yeah is there any best practices that we follow in order to choose the right pivot the right pivot uh we'll discuss there is algorithm that is called median of three okay so that uh reduce this computational complexity of 14 around okay in real scenario and um that that is the case but the simplest way was choosing the first one so that i have started but you can choose middle you can choose uh last one or highest okay so that is up to you so you have to choose first index last index or middle middle index that are the three possibility or even you can choose randomized one that is called as the randomized version of the quicksort some other things are also there but in that case the selection of the pivot is you can choose randomly a number so you can follow these steps then you will surely reach to the that partitioning label okay okay so that we will discuss you know how we will choose an optimal by what okay thank you hi sir good morning good morning yeah already my question was uh resolved the last part of your solution okay thank you thank you okay i guess we are good to go so just one question yeah i think uh extending to the question of bikesh so i was writing but the chat is not involved now so when the system scales right uh in a real business uh problem when the system scales we have millions of the record and when this swap happens uh in in that notion the kind of computational uh stuff when you actually sees that yeah your left is uh comparable higher than the right it moves from the swapping mechanism yeah doesn't that swap also have a uh complexity extending to that just not the loop that you mentioned that is while loops right it has a nested by loop but on the same side the swap will also occur a complexity isn't it so in in the kind of scale but but when when let's say you're talking about millions of of number n numbers okay so is that comparison that this swap will yes of course it will incur some some cost but it will be much lower lower than that of the main cost as are taken by the algorithm and log and that is and is very large millions so in that swap operation will yes of course it will consume time and you can say resource but it will be less lesser to that of the actual resource being consumed by the algorithm so I'm not saying that like we have to eliminate any operation but we have to identify the dominating one correct so i mean the same extension the same answer you say i gave so we are not any we are not anyhow leaving out the operation that is swap operation so how we optimize in a real time i mean this could be something where you have millions of exchange which is happening yeah yeah so how we can optimize that okay so actually what happened we we we use this quick sort and other steps primarily for the you can say primary memory operation not in the databases for databases there are designated trees as well so that are quite efficient then these these things so that we will discuss separately okay just these are used for the for the main memory operations only we are not bothering going in the secondary storage to access the element by this understood sir understood thank you thank you so much hi sir good morning i have one question yes so for in the in the case of the merge sort as well as this um the sort in quick short the number of comparison is same correct because in both the cases only difference is in the case of your merge thought you are dividing it to the n number of the smallest elements but in this case you are just picking up a randomly one pivot element but the comparison remains the same yes yes comparison remains the order of n same yes thanks hi sir good morning I have a question sir how does a quick sort behave when there is a duplicate element within an array so what will be the most appropriate partitioning scheme in this case okay so when we find a duplicate element if you see we are saying the left is less than or equal to right sorry so so that case you can assume a of left is less than pi vote so equality sign because if you see inner while loop so i had not mentioned any equality sign so that's why your question is coming i guess so in that case you have to do a minor modification either on the left sub half on the right sub half you have to use the equality sign so then then that problem will automatically resolve okay thank you a of left is less than or equal to pi vote or a of right is greater than or equal to pi vote so that is there yeah it makes sense doesn't the line number four doesn't the line number four state the same thing that while you're no no so this right and this right and left are the indices zero one two yeah these are not the errors yeah understood understood okay okay we are good to go i hope yes so just uh see this visible once i upload this we will just see how the things are happening so it is quite clear and an important suggestion for you all i guess you don't you people will don't get any time just just use pen and paper open the my ppt algorithm just do a dry run okay just do a dry run for this if you are getting everything done then fine that is more than enough for to you to understand the problem okay so just every time try to do a dry run so that is very beneficial in case of algorithm like subject okay we are just doing a dry run and that is helping us a lot now if you see talk about the worst case worst case is what one two three four five six seven eight I have taken this array that is already sorted so I will choose pivot as this is position will remain fixed I will get remaining and minus one element then I will get n minus 2 element, then I will get n minus 3 element. Every time the extreme part that is index 0 element is cutting down. So, in that case, the inner while loop will also run for n minus 1 time and we will reach to a computational complexity of order of n square rather than order of n of log of n. So, this is the worst case discussion that this is the you can say recurrence relation okay and this is how the algorithm will behave if one one element is curtailing down and total computational complexity reaches to order of n square therefore it is recommended that the selection of the pivot player pivotal role in the quick shot algorithm where if it is wrongly chosen as the smallest element so it will okay every time i am randomly choosing a pivot and it is a smallest element it is the smallest element okay then what will happen uh sub array size will be n minus one server size will be n minus two and so on so the the the if if if it is best the best part of quicksort will be when your pivot is middle element in most of the cases but we will try to make the pivot such that it is middle part of the savary middle part of the savary middle part of the savary then we are in the best and best case and some near to the middle then we we are in the average case but worst case means that we have chosen the extreme element either highest or lowest every time so that's why this is a famous question what is the worst case complexity of the quick sort we are seeing order of n square okay so in average case this is the recurrence relation 2t n by 2 plus okay order of n i'm assuming greater value okay so that is 2t n by 2 2 equal half we are dividing and then we are using these main number of comparisons okay so this recurrence relation is as equivalent to the merge sort but this is only applicable if the array is all it not sorted already okay if array is not sorted or we can say that even not fairly sorted even not fairly sorted okay so then this average case or best case applies so this is the fastest known algorithm in practice these are the two cases that is n log n an order of n square n square because every time we are getting the extreme elements okay and this is one scenario so here how pivot selection is happening so v is the pivot we are choosing s minus v and s minus v for both the extreme ends so if you visualize how this problem is so there is a pool of number if i choose 65 as the pivot then automatically the result will be likewise okay and so on we will do the partition and ultimately we will get back the result so therefore this approach is also a divide and conquer but here division mechanism is a selection of the pivot now the thing is issue of consideration is how to pick a pivot so there are many methods okay so this one we will consider is known as given good result and to be easy and efficient we will discuss the partition strategies first okay so the primary objective of picking a pivot is choosing a pivot so that we will get two partition of almost equal size that is important okay so uh one way is choosing first element as pivot that already we have did next is random selection next is median that is n by two ceiling or floor that what we have did in my certain binary and the important one is median of three partition median of three partition that eliminates the bad cases of sorted input and reduce the number of comparison by 14 percent reduce the number of comparison by 14 percent so what is the basic idea if i had an array with number of elements okay and what i will choose i will choose first i will choose last and i will choose middle i will choose middle okay and during selection of first middle and worst what it said that first should be less than middle and middle should be less than last this is important thing so if it is not in order then we will perform number of swaps okay so that we can maintain this order so median of three is the approach which ensures the selection of first middle and last element and their order is first median and last if it is not in order then we will perform number of swaps and other thing and then we will reach to our solution so if you see the algorithm center is left by 2 if a of center is less than a of left so the condition is violated so we will do a swap if a of right is less than left we will do a swap if a of right is less than center we will do a swap so finally what we will get then if you see one example here we have 2 13 6 so this is okay but 6 will come here and 13 will go there after applying these algorithm steps okay And if you see here, there is written that place the pivot at position right minus 1. So, after selection of the pivot, now we have selected 6 as pivot. It is swapped with the second last element, that is this. And now we will start the quicksort algorithm. Now we will start the quicksort algorithm, okay. So, ways choose low, high and median element. Maintain their order by doing the swapping. and let us say I have chosen the median element as pivot or this element as pivot, swap with the second last and start the quicksort operation. So, this is the algorithm and these are the steps. Okay, again tomorrow I will recall this one and then we will shift to the interesting approach that is called as the maximum subarray. This is again a divide and conquer algorithm and the best part is that the three cases that is brute force divide and conquer and dynamic program all simultaneously held for the problem of maximum seven that we will start tomorrow and also i will try to again summarize this but this is an interesting algorithm so this is you can say prerequisite for the quick thought so that it will not reach to a worst case scenario okay so if someone have some query let me know otherwise we'll close the session those who are done can leave the session hello sir good morning yes good morning yeah sir actually uh if we see that complexity for marsort and quicksort both are uh same for the average cases so um quicksort i think that for um quicksort is more complex so why we are introducing the quicksort means we are we have to know that yeah yeah fine fine it is complex for the worst case scenario but for average and best case it is it is taking n log n and do not require any swap in swap out operation with the external memory okay in same memory it is doing so therefore it is fastest so therefore it is popular and worst case so um arises in very rare cases we never use the quick sort on a sort already sorted array because that is an assumption if it happened then we are going to order of n square otherwise it is faster than merge sort okay so if both the complexity is same so it is faster than match sort for average and best cases yes yes so that's why their order is same their order is n log n but it is faster okay sir thank you sir i have a question so while explaining the average case you mentioned that uh array should be fairly sorted or or it should be shorted so why is it it should not be fairly sorted and um if it is sorted then worst case if it is fairly sorted we are close to worst case oh because sorted means that if we pick a pivot then obviously the remaining half will remaining all element will be there okay got it thank you sir good morning sir good morning so uh in the quick sort we are using recursion yes but the person requires the additional memory because each recursion function call is stored in the quality stack that is the internal memory that will be vanish after the execution of the algorithm yeah if we remove recursion and implement quicksort by using loop or iteration how does it affect the time and space complexity of the algorithm so if we if we use uh looping or something else so uh so of course that memory requirement will be vanished automatically okay and space complexity if you talk about so i'm not bothering about the temporary storage that is being acquired while execution of the program i'm bothering about there is an array and result was stored in the same array in case of merge sort there was an array and result was stored in the another another array so that is the difference okay and for seeing the actually how the things are changing so you can do a memory analysis and run time analysis of a large array so in any small you will not find or there are some sort of libraries in Python that count the space and time required for the execution so that you can do and check how it is influencing so clock time and memory requirement you can directly witness so in Python there is a provision of fetching out the time required for execution and a memory requirement for execution so you you can directly apply and check how it is working this I guess we have solved the queries and we are good to close so thank you everyone RCS is having some query I'm enabling my for you your mic is already enabled as is okay hoping there is no query thank you all and we'll meet tomorrow