Hi, everyone.
So in this video, we are going to discuss this question, Jack, if the phrase altered using recursion,
we can easily use all this question using for loop or by loop.
But since we are learning recursion, so we will use the occasion to check if the errors are dead or
not.
So input will basically be very.
And we will take how many elements are present, so the size of the area and the area itself, so since
we are going to use a caution.
So first, let us discuss about the base case.
So what would be the best case if the number of elements is zero or we can say the number of elements
is one, then in that case I can say my area sorted.
So basically, they're the functional people I will return, true or false, whether that is sorted
or that is not sorted and my function will take that as input and it will take how many elements are
there inside the area.
And this will be the best case.
So if that is an element or the area has only one element, then I can say my area sorted.
So basically in this case, I will return to address what we need to do.
So this is already.
So for using that occasion, we need to split our big problem into smaller problems, so there are two
ways for breaking the area.
So first, which is basically this one.
So I will work on this small part of the problem and the commission will work on this part.
The second way of making our ready to use U.S. is basically this way.
I will work on this small problem and the commission will work on this big problem, so there are two
ways of splitting the area.
So there are two ways of making our big problem into smaller problem.
First, using this way.
So I will give the commission this area to solve.
And similarly, in this way, I will give you a commission decided to solve.
So first, let's discuss this way.
So what do.
So this is area.
So first of all, I can check.
So this is my 08 and next the next one in next to and so on.
So first of all, I can check whether AOF zero is Gherardini of one.
So if this is a condition, that means there is not totted.
So in this case, I will directly return false.
I can actually done false from here and is what we need to do.
So this is already.
So I am checking the 08 element and the first element.
So if the root element is greater than the first element, I returning false in the end.
But what I will do so in the end part after comparing these two elements, so I will call the kitchen,
I have worked on this small problem, so I will call the kitchen on this big problem and the kitchen
will return me a bowl value, whether this array, this small area is sorted or not.
So recursion will return me a true or false value and I will return that to Werfel's value.
Simple.
So let's ride the gold.
So the name of the file is precautionary because in an edit or CBP and what we need to do.
So let's create a function of the functional people and let's say the name of the function is is sorted.
So what this function will take, it will take out as input and it will take how many elements are there
inside the room, basically the size of the area.
So this case is very simple.
If.
If my air is empty or.
It is containing only one element, so in that case, what I will do, so I will return to my area,
sorted, simple.
So this is simple biscuits and now what we can do.
So what do we do now?
So if this is my area.
I can't compare the 08 element and the first element, so if the 08 element is greater than the first
element, then I can say that is not certain.
I will return false without calling records and there is no need to call the occasion.
So let's check this condition.
So if AOF zero is the first in the 08 element, is then the first element, then we know the answer
that the error is not sorted.
So we cannot let it unfold from here.
Otherwise what we need to do.
So what I'm doing.
I'm comparing the 08 element and the first element, so my small work has been done.
I have completed my small work, so now I have worked on this small problem.
So I will call the guys on this.
Eddie.
So this is a then they are easy.
So what is this?
This is a plus one.
Remember pointers.
So you should remember pointillism.
So this is a plus one.
So I need to pass this, Eddie.
And here, the size of the area was and so what is the size of this area, the size of this area and
minus one because of this one element?
So this is one element.
So the size of this area and minus one.
So let's call occasion on this small area.
So Boole is smaller.
And assorted.
So what I didn't do, I need to collocation it started.
I need to pass this, Mollari smaller is E-Plus one, and the number of elements is reduced by one,
so end minus one.
So this function takes areas input and the sizes input and it will return, whether that is or did or
not.
So basically that equation will return, whether this area is sorted or not.
So after getting that.
So if.
If that is sorted, so if the small at sorted, I will return to.
It's a small area sorted, I will return true in the early part.
I will return fault if the smaller is not sorted, then how can the three can be sorted?
So the part I will return false.
So what I am doing here, so the value of is smaller is true.
If its value is true, then I am returning true.
If its value is false, then I am returning false.
So basically, can I write like this.
So let's come and do this one.
So basically, I can write like this, basically written.
Is smaller started, so this is exactly the same, so here, if that Ismailov sorted that true.
So I was returning true.
If those smaller, that is false.
If these smaller is not ordered.
So I wondered I was returning false.
And this is exactly the same.
If it's really true, I will return true.
If it's values fault, I will return false.
Simple.
So now let's test our function.
Let's create another.
Let's say the elements are one, two, three, four and five.
So let's call the function.
If it started.
I will give Eddie and his number of elements his five.
So we can print the message.
Sorted.
And the part I can bring the message.
Nazar did.
So let's see what will be the output.
So let's send our file.
So basically, the output is coming out to be sorted, so let's see how it is working.
Let's write a novel called.
So basically, this is the area that is one, two, three, four, five, so my area is.
One, two, three, four and five, and the number of elements is five, so this function is taking
out as input and it is taking how many elements are there.
So the first step is basically size is not zero size sizes, not zero size is not one.
So zero element is bigger than the first element.
No one is not bigger to do.
So call on this smaller.
So I will call on smaller.
And the smaller is two, three, four, five, and the number of elements for.
So then size is not zero sizes, not one, though, is not greater than three, so I will call on the
smaller.
So I will call on similary, three, four, five.
And number three, so three say it is not this is not one and it's not good than four.
So I will call on this small area.
So I'm calling on four and five.
So now size is two.
So size is not zero, size is not one and four is not bigger than five.
So I will call on this more, Larry.
Size is one, size is not zero, but size is one, so size is one.
If that is one return, true, so dysfunction will return to.
This function will return true, so basically I have worked for this problem, so basically for a small
then five and smaller array sorted.
So I will return to.
So I compare 2004 and then I called on this small area, so small areas are done through this audit.
So that means the big areas also saw.
So I will return.
So, too, is basically smaller than three.
So I worked on this problem and I called the equation for this area.
And this area is basically sorted.
It is running through.
So basically the big areas also sorted.
So I really don't know.
So I compared one and two.
So one is smaller than two.
And this area told me that this small area sorted.
So the dining room for this small area.
So that means the whole area sorted.
So I will return to.
So that's how this function will return true for this area one, two, three, four and five.
So if it is returning true, I am printing the area sorted so the output is sorted out.
Now, let's take one more example in which case the area is not sorted.
So let's say let's replace the values of this is five and this is four, so one, two, three, five
and four.
So for this I should get the arrays not sorted.
So let's run our file.
And the output is not sorted, so let's see how it is working.
So basically, you are passing the area, so you are giving this area one, two, three, five and four.
And the number of elements is five, the value of five, so you will compare.
So five is not zero, five is not one number of elements is not zero, not one.
So it will call on smaller.
And first of all, before calling, I am taking zero tolerance in the first 10 minutes.
So I will check one or two.
So in less than two.
So I will call on this Mollari E plus and minus the number of elements is four and the smaller is two,
three, five and four.
So the size is not zero, the size is not one who is basically less than three.
So I will call that equation on smaller.
Very.
So I will call that equation on three, five and four number of elements, history and minus one.
So size is not zero, size is not one and three is less than five.
So we will call on the smaller.
So call on this, Mollari, the elements are five and four and the size is two, so size is not zero,
size is not one, and five is basically bigger than four.
So five is basically bigger than four.
So I will return false.
So I will return false from here.
I will return false from here and then what will happen?
So this is right is less than five.
So the question is telling me that this smaller is not sorted.
We are getting false.
So if the smaller is not sorted, how can the big three will be sorted?
So I'm likely returning false.
You can see I am actually returning the value.
I'm returning the small sorted area.
So if this area is not sorted, if this is false, I will returning false.
So again, so this was true.
I worked for this problem.
So too is basically less than three, but the small is not certain.
So the smaller is not smaller, is returning false.
So if it is returning false and likely returning false.
So if this is not sorted, how can the bigger they will be sorted.
So I am returning false.
Similarly, when is less than two and recording is telling me this area is not sorted.
So how can that be sorted.
So I am returning false.
So if I am returning false.
So if this is false I will go into l'esprit and I will print not sorted.
And you can see the output is not sorted, the output is not sorted.
So that's how this function is working.
So I told you there are two ways.
So one more thing.
So how we are calling our problem, how we are calling it question, so I'm calling recursion like this.
First time checking the 08 element and the first element, and then I'm calling that equation for.
This area, so you can do the opposite also for this problem, what you can do first.
First, you can call dedication for this, Mulready, and then you can check 08 and the first element.
So let's do that.
So let's create another function.
So Boulle is slated to.
So it will take ideas, input, and it will take how many elements are present.
So we can just copy the best case, this case will remain same, if that is Amberleigh or less containing
only one element, then in that case, my order.
So what I want to do here is so in this function, first I am taking this route and the first element
and then I am calling the equation for the small area.
But for this problem, what you can do first, you can call on this, Mulready.
So let's call for this, Mulready.
So let's copy this line.
I am calling medication for this malaria first time, checking whether it is malaria sorted or not.
So you can right here.
So if.
This smaller that is not sorted.
So if smaller aid is not sorted, then you can return false.
Simple.
Otherwise, so what I'm doing here is first I am calling the equation for the smaller.
So if the smaller that is returning false, if the smaller is returning for the smaller is not sorted.
So you can return false.
OK, this is not the bigger is also not certain.
So if it is returning to.
So if we will reach this land line number 29, so if it is returning true, then we need to check this
root element.
And the first element also, we need to do the comparison, so let's do the comparison so that this
line is sorted.
So now let's compare the zeroth element and the first element.
So if you the any of one, I will return false.
That is not sorted.
Otherwise I will return to the Elzbieta I will return to.
So the working is exactly the working is nearly the same.
The working is nearly the same.
So here's what we are doing.
So first, I am calling the equation for this Eddie, for this malerie plus one and the number of elements
and my.
So that's Millerick and misogynous malartic and not order.
So I have the smaller days and I thought it was the smaller array sorted through.
So if the smaller area is a standing fault, so if it is retaining falls is a smaller is not then the
bigger is also not shorter.
So we are turning faults otherwise if it is returning true.
So no, I don't mind if this smaller is returning true, then I need to compare this element and the
first element.
So if zeroth element is less then first element then the computer is ordered.
So I'm returning.
True, otherwise I am returning false.
So let's test this function also.
So this is a better way to write the code.
It started to.
So let's call the function is started to.
And the output should be not sorted.
So the output is not sorted, and that's just for sorted, Eddie.
So this is four and five.
So our protests started.
So this is a Saturday, so I told you, there are two ways of making our.
So remember, initially I told you there are two ways of breaking our area.
So the first was basically.
I will I will solve this problem and I will apply the commission on this area the other way of solving
the problem that their way of splitting our area into two parts, basically.
First, I will work on the last problem and I will work and dedication will work on this small area.
So we have written the code for this.
Now let's write the code for this, Eddie.
So what I want to do, what I want to say it is see?
So initially, I was comparing the zero element in the first element.
Now what I will do, I will compare the last element and the second last element.
So the last element has mix and minus one, and the second last element has an X and minus two.
So if F and minus two is good, then minus one I will return false.
That is not sorted.
Otherwise, if this condition is false.
What I will do so I will call Dacogen.
I have worked for this small problem.
Now I will call it equation for deciding and recursion will tell me whether this is sorted or not solved
because it will return true or false value and similarly I will return that value.
So if this small array sorted and we already checked this smaller problem, so the computer is sorted,
you don't know if this idea is not certain.
If the similarity is not sorted, then how can the big three will be sorted?
So I will return false.
So I will directly return whether I will return the value.
So let's write the code.
So the name of the function is is sorted three.
So, Abul.
Is sorted three, it will take.
And it has endured and it will take how many elements are there, so be it case will remain the same
if that is empty.
If that is an.
Or it is containing only one element then return true, otherwise you need to we can compare.
So if the second last element.
Is greater in the last element.
Then I can return false that marriage is not sorted otherwise for Tulu.
We will call it equation.
So I will call Dacogen on this already, so let's call verification on similary.
That is still a and the number of elements and minus one.
So what I'm doing here is, see, this is our area.
This is a.
And I am comparing the second last element and the last element, so I'm comparing the last and the
second last element.
So if.
Now, that is correct, so if the if the last element defense minus one is the last element is greater
than the second last element, so I will reach at this line.
So what I'm doing here is I am calling the recursion on this area and for this idea how we can pass
this area.
So how we can pass this added to the recursion.
So we need to pass it and the number of elements so you can see the number of elements.
And minus one, because we removed we are not passing this element.
We are passing only this area.
So the number of elements will reduce by one.
So that's why at it and a number of elements.
So N minus one third equation will tell me.
So the equation will return.
True, if this is ordered, recursion will return false if this array is not sorted.
So if recursion is returning false.
So if this is not sorted, then the bigot is also not sorted.
So I will return false.
So that's why I am returning directly.
So if this area, this model that is not sorted, then the results are not sorted.
So I likely returning.
Similarly, if this a small area sorted, if the smaller is sorted and we already checked for this condition,
we will reject these elements then this sorted and these elements are in the correct position.
So I will return to the also.
So that's why I am returning directly.
So now let's test this function is Sawtell three.
So the output should be true for one, two, three, four and five, so the output is true and let's
see how the code is working.
So let's take the example.
One, two, three, four and five, so one, two, three, four and five, the number of elements is
five, so size is not zero, size is not one and basically four is less than five.
So I will call on this, Mollari.
So I am calling on smaller and minus one.
So the elements are one, two, three and four and the size is four sizes, not zero sizes, not one,
and basically three is less than four.
So I will call on the smaller one to three then monofilament history.
So basically size is not zero sizes, not one and two is less than three.
So I will call on the smaller.
Smaller is one and two and sizes basically two.
So basically the size is not zero sizes, not one and one is basically less than two.
So I will call on the smaller three, which is one size is one.
So size is not zero, but size is one now, so size is one.
Now if the value of size is when you will return.
True.
So I will return to.
I am returning to.
So basically, this small area is sorted and we already checked for this condition, so the computer
will be sorted, so I am returning.
True, we are likely returning to.
So this small area sorted the small area, so that's a big area will also be sorted because we already
checked for two entry.
So I will return through again, this small area sorted.
So big area will also be sorted.
So I'm returning through.
So this small area sorted we are getting through from the guys into the bigger area will also be sorted.
So I am returning through.
So that's why.
So that's how it is working.
So through means sprint sorted.
So now let's test for when I would add is not sorted.
Let's replace the value.
So let's say this is five and let's say this is three.
So now this time I should get nauseated so the output is not sorted.
Now let's again test let's but I don't have a code and let's see how it will work.
So basically, this is.
I already went to five, four and three, the number of elements is five, so I'm checking.
So five is not zero, size is not zero, size is not one, then I am checking.
So basically four is a good entry.
So four is basically a good entry.
So that is not certain.
If that is not sorted, I will return false.
So I will likely return false from here and this one loose fault.
So I will go in the expert and I will bring not sorted.
So let's do one thing, let's make it two and let's make it one, the one winwin and then let's see
how it will work.
So let's make it.
To here and let me get one here, and that is true, that is.
So this is.
This is three, four and five for this area, this area is also not sorted, so the output should be
not sorted.
So the output is not sorted.
And let's see how.
Let's let's.
And have a good.
So this is the area to one.
Three, four and five, and the number of elements is basically five.
So sizes in all sizes, not one of four, is less than five, so call on the smaller, smaller is to
one, three and four.
Number of elements for size is not zero sizes, not one and three is less than four.
So I will call on this, Larry.
Smaller to one entry number of elements, history, so size is not zero, size is not one and one is
less than three.
So I will call on this smaller, smaller arrays to number of elements to.
So size is not zero sizes, not one.
But this is wrong.
So two is bigger than one.
So two is bigger than one return false.
So I will return false from here.
Now, this area is not sorted, I'm getting false communication, so this site is not sorted, that
is the big area is also not sorted.
So I'm likely returning, so I will return false.
So this site is not sorted.
Who?
One tree is not sorted.
So the computer is also not sorted.
So I will return.
False.
So basically, this Eddie.
Is not sorted.
I'm getting calls from The Guardian, so that means the bigot is also not sorted, so I will return
false.
So if this value is false, I will go anywhere.
The but I will print not sorted, not sorted is the right output.
So that's how everything is working.
So we can do so what we are doing here.
So this is only just one variable.
So what is the value of this variable.
The value of this variable is this one.
So you can just like look at it and you can post it here and there is no need.
To create this variable.
Simple, so this will work is hard, it will work, you can test it also, so the output should be not
sorted, the output is not sorted.
And similarly, if that is sorted.
When?
Two, three, four and five, so the output will be sorted now.
Now, police are now.
So we were creating this extra variable is more or less.
So it was used only once.
So why to create variables we can just basically return.
OK, so this is it from this video.
I will see you in the next one.