Note 20240702 1401 Otter - Ai
Note 20240702 1401 Otter - Ai
is actually quite a simple algorithm. And once you understand it, you will get it.
But first, you know, it might seem a bit hard. So the first thing you do is mark
the first element as sorted. So you need to assume that the first element is
sorted, and we start with the second element. So we start here is the second
element. And let's start it. Why is it so like and now what we're going to do is
compare the so the second element is the current element. There's one and compare
that with the sorted elements, right, and then ship all the elements and the
position two is greater than the current element to the right by one position. What
does that mean?
And then after we compare, we're going to extract so we're going to start with the
second element. Extract the element by extracting an element, we're basically
saving this to a variable attack the way we did it before, you know, and then we
will compare it with the elements that are already sorted. So wait, let's just
first mark the first element is sorted. Yeah. So the cream basically means that
it's sorted No. So now we're going to extract it basically meant with saving it a
tab. And if the element is greater, then we will shift the positions but now we
don't really need to ship any position. We can add it back here now 23
We are going to extract this element. This is bad without the orange one is
disordered section ACTEC. Right. So
this is disorder section, and this is our current element. 23 is our current
element. Now what we are going to do is we're going to extract this done and I'm
going to compare it with each element in the sorted section. And we're going to
compare with 28 and 40. So, if the element in the sorted section are greater than
we will move the element by one position by right okay. Okay, Tony, let's swing as
we're moving one, this is about right. And then we'll put this back here. Is this
greater know that there's only 340? No, it's not. So we just kind of let let it
stay there. We're gonna put on a tree here. Okay. Now let's talk about 19. So we're
going to first extract element 19 is our current element. And now we're going to
compare it with 28. First, as to an eighth grader. Well as Gregor so we'll move to
the eighth one position is 23 greater. Well, it is greater so we'll move it one
possession is 14 greater or not? No, it's not greater. So we'll just put it there.
He put it we'll just let 14 stay we'll put it here. So basically, that's the
algorithm will do this forever. So we will compare it with each of these elements
and then we will first extract the element from the unsorted section. And then
we're going to compare it and then put it there. Now here No, 32 right to redo it,
redo is not very interesting. So right there. 33 is already sorted. Okay, let's try
1515 is let's put 15 here. We're not actually compared with each element, you're
actually comparing with the element that is the greatest Okay, that's interesting.
Okay, okay. That's interesting. Anyway, in order to program instructions, or it's
actually quite simple. The textbook uses a swapping algorithm in it, but I'm not
sure. But the past papers don't actually do that. So I'm just going to go with the
proper method, let's say insertion sort. Then temp as integer. So this is where we
can say extract the current element, okay. So, for position equals 02 We will
usually we start from zero right? Now we're going to actually start from one one to
minus.my array, my array dot length minus one as I have already explained, the
reason we use my array dot length minus one minus one is because we don't that is
zero indexed. Anyway. So this is the loop for which we are going to go from the
first the, the second element to so on, right? And now we're going to compare it so
we're going to compare while position is greater than, greater than zero. And the
reason we could position is greater than zero is so that we don't go beyond we
don't go beyond the last one. Now why why position is greater than zero and minus
start? Well, we actually even had to wait current list partners current current if
it's actually evolved to minus deposition. Make sense? Right. So this is this is
what we mean by extracting it. By extracting we're basically saving it here,
extracting. Now we're going to compare this and my current is current. Great. Less
than the position my array dot position, minus one, so we're comparing with the
second element. And then if it is true, then we will move the move each elements to
the right by one but how do we move that we actually move it this way? My array
position is actually important to my array, radar position magnet. So this is
basically moving the element right by one. And then what we'll do is position the
ball position minus one. This will keep track of how many times we have move it.
And this will also mean that this will basically keep track of how many time we're
moving and then we put this here and my list start position. And now we're going to
put the element back myself position. My new stock position is actually equal to
the current and this is basically putting the argument back. Now, one thing you
have to understand ANSYS basically like a cork on VB dotnet itself is that usually,
if we have a Boolean comparison a and Boolean comparison, let's see. A and B. Now
if A is false if A is false, what happens is it don't actually go around and
checking B anymore and that that actually makes sense because false. And we don't
really care whenever the value false and the other value is false and whatever it
is false and true is also false, false, false and false is also false. So what if A
is false, then it doesn't actually check B anymore, but the way VB NET works is
that if A is false, it still goes and check be I don't know. I don't know why it
does, but it does. So what So what the problem that can happen here is that what
the position is less than zero already, it might go and access the array. Let's say
if the position is less than zero, let's say if the position is negative one, we'll
go and access this position, let's say negative two. Well, my array index negative
two, well index negative two does not exist. So you will get an index out of the
bounds error. So instead of doing that, what you have to do is you have to write
and also write and also return that will not check the second value anymore. If the
first condition is false, then the second condition will automatically will not be
checked anymore. So that's why we use an also here and this is basically the same
algorithm. The textbook actually uses a swapping algorithm. I don't know why it
does, but it uses the swapping algorithm inside and you can also use swapping
elsewhere. I don't but I prefer this method, but this is much better. And it's
easier to you know, it's just easier like why would you swap this and he also uses
like a lot of like and uses one for loop and it is this knowing you know what I'm
saying? So this is just an annoying way of doing it. But this is a more official
way of doing it can say anyway, now that we're done with all the swapping, awkward
searching, sorting and swapping and all this while we're dumps we will be doing a
few past papers. And in these past papers, we will be we will also focus a little
bit on File Handling. I'll handle it and then we will focus we'll do like two or
three questions. And then we'll do some file handling in concrete can cooperate
File Handling and this these questions and then we will be done for all this
algorithm. If you have any questions. Questions, you can ask me in the link. You
can ask me wherever you want actually you can you can call me you can contact me I
think it's in the link in the description. As always. Another thing is okay at the
start of the at the start of the stuff I need to actually explain to my length my
position also explain
Yeah, this is actually on get up. I have a base as I am writing this. I'm probably
going to be pushing this to get up so yeah, that's that also. Thank you
Transcribed by https://otter.ai