DSA in Python: Insertion Sort
Transcript
English (Auto-generated)
in this case we are going to implement um in session sort algorithm using python. Before we proceed with the implementation, let's have a look at, let's have a look at visualization of the algorithm. Okay, uh this is the record of algorithm and the steps are fairly simple at each. At each step we are comparing the current element with the previous element and if the previous element is greater then we shifted forward and place the current and the players the element Current element at its place. Let's say we have, we had 21, we compared with 24, we shifted 24 forward and we placed 21 at its place. Similarly if by the way we check it with all the previous values until we find a value which is smaller than the current element. We'll get into an another example while implement before we implement the algorithm. So let's shift towards the implementation. Yeah. Okay, let's have one. This bread from element one And step two is compared with previous Belgium P is if previous values greater than the current value then shift previous value forward and and Mhm pull back step let's have let's look at an example, that's what we have 123 um 55 of lecture six and german. And then five, we'll compare it with salmon and Will move seven forward And then we will compare it with six because six is better. Will move six Will shift six forward as well and finally will add else less current value at correct place. So we'll place five over here, that's it. This is the process. Um now we will implement it. Session sword. We have to find a method and there will be two loops in this algorithm. The first the outer loop will be to traverse that, let's define it. And the second loop will be to shift elements forward to place the current number at the correctness. We'll use value for that. Um First let's store current delivery in this whatever and then what else, what else do we need? We need index variable for That will be that will begin from I -1 because we are looking at the previous one, we'll keep on doing this if you want a lemon is smaller than the next government than the previous Senate and that is and if Bill jay is better. Let's see and that can't be negative that way. What we will do will chef Pellman forward plus we will update index variable. So what's happening over here let's say we have +1236 and chairman when we have that's fiber there, that's how we look look at Salmon moving forward and if we upgrade the as well next year will be to place the element of the correctness that is This one is a cradle good. What we are doing over here is We are shifting elements seven and 6 forward and finally we will place five word correct position when it will get over here. This is our algorithm has been completed and now we'll return that uh alert extra. Mhm alert define another 100 this one then and first close. Mhm. Okay. And finally print the reasons. That's ironic. So there is an error. Let's see where what went from. Okay, uh it should be a land profit. As you can see it looks all the elements have been ordered. Let's look at why do we have? Okay, let's look at the time complexity of the centers and we have these low veterans and times and we have this loop which in worst case runs and end times. So the time complexity will be and square and into an and square. Mhm bigger will be in the worst case scenario. What was the space complexity? It'll be why concern because we don't need any space to implement this algorithm. We have done everything in place using this array, we need this variable I guess current element, it doesn't depend on the size of the array. So uh the space needed is is going strength and this is it. We have implemented in session sort and looked into the complexity details and we'll get into more algorithms and next cost I'll see you there. Bye bye.