Insertion sort - Pseudo code _ Applied AI Course

The Art of Understanding a Simple Sorting Algorithm

When it comes to sorting algorithms, there are many different approaches and techniques that can be used to achieve efficient and effective results. In this article, we will explore one such algorithm, known as quicksort, and examine how it works in detail.

Let's start with an example of the code itself, which is quite simple to understand:

```

while I is greater than zero

my six got copied here

my six replaced my five right then I equals to I minus one

so what happened to my eye my eye became zero now if this loop again restarts because this is a loop right this is the loop now here while I is greater than zero no I is equal to zero now so I will come out of this loop as soon as I come out of this loop it says AI plus one equals two key now what is AI plus 1

```

As we can see, this code appears to be a simple loop that increments the value of `I` by 1 until it reaches 0. However, upon closer examination, we realize that there is actually more going on beneath the surface.

The first part of the loop copies the current value of `my six` into itself, and then replaces it with the value of `my five`. This is a clever trick to avoid having to use an extra variable to store the current value of `my six`.

Next, the code calculates the new value of `I` by subtracting 1 from its current value. This results in `my eye` becoming zero. However, the loop does not simply stop here - it actually restarts because this is a loop.

The inner part of the loop checks whether `my I` is still greater than zero. If it is, then the loop continues indefinitely. But if `my I` is equal to zero, then the loop exits.

Now, let's examine what happens when `J` takes on different values. We will take the example of `J equals 2`, and walk through the steps of the algorithm:

```

at the end of J equals 2 at the end of my first outer iteration

my first so this became five

so what is my area of my array now

is five six three one eight seven two four

```

As we can see, the value of `my first` has increased to 5. This means that the current subarray being sorted is now `[5, 6, 3, 1, 8, 7, 2, 4]`.

Next, we examine what happens when `J` equals 3:

```

at the end of J equals to do this is the this is the array that you have now

take J equals to 3 because this loop is running right now try to write the same thing on your notebook so then you know how these so how this inner loop and outer loop are working in a nutshell what's happening is so so you want you want to do that walkthrough on your page on your notebook so that you understand this code better right

```

As we can see, the value of `J` has increased to 3. This means that the current subarray being sorted is now `[4, 5, 6, 3, 1, 8, 7, 2]`.

We can continue to examine different values of `J`, and walk through the steps of the algorithm. Each time we do so, we gain a deeper understanding of how the quicksort algorithm works.

Let's examine what happens when `J` equals 6:

```

when J equals to 6 I am trying to take the sixth element and trying to insert into this array which is already sorted right

when my J equals to 6 a 1 2 a 5 are already sorted so I'm taking the value of a 6 or AJ into key right and I will start from a J minus 1 from here from here rest of the elements this is exactly what the intuition says right

```

As we can see, when `J` equals 6, we are trying to insert the sixth element into the array. The array is already sorted, so we know that the new value will be inserted at the correct position.

We can continue to examine different values of `J`, and walk through the steps of the algorithm. Each time we do so, we gain a deeper understanding of how the quicksort algorithm works.

The key insight behind this algorithm is that when `J` equals `n-1`, where `n` is the length of the array, we know that all the elements in the subarray are already sorted. In this case, we simply take the value of `AJ` and insert it into the correct position in the array.

To achieve this, we start from `J-1` (which is 5 in our example) and iterate through each element in the subarray. We compare the current element with the value of `AJ`, and if it is smaller, we increment `my I` by 1. If it is larger, we simply move on to the next element.

By doing so, we effectively "partition" the array into two parts: elements that are less than or equal to `AJ`, and elements that are greater than `AJ`. This allows us to recursively apply the same algorithm to each part of the array, resulting in a fully sorted array.

In conclusion, the quicksort algorithm is a simple yet elegant solution to the problem of sorting arrays. By understanding how it works, we can gain a deeper appreciation for the underlying mechanics of this algorithm and develop our own solutions to similar problems.

"WEBVTTKind: captionsLanguage: enso in the previous video we understood the intuition behind insertion sort and we took a very simple example like this to understand what actually is happening insertion sort right so now let's try to move to some code right let's actually write down the code of course as important as intuition is equally important is the ability to convert that intuition into code now here we had a cup we had a few choices we could have written this whole code in any major programming language we could have written it didn't see C++ Java Python right or more web based languages like JavaScript or Ruby we would have written the code in any of these programming languages but we decided not to write so we will actually understand all of the code using a pseudo code now the pseudo in English the word pseudo basically means false so I'll explain you what the pseudo code is and why we are choosing it but the reason why we do not use any specific language is because some of our students might be good at C right if I so if I chose C some of our students might might have learned whole of programming from C some students may not know C but they might know Python or there could be some people who are web developers who know JavaScript who don't know other languages very well so because we have wide spectrum of students from various backgrounds we felt it is not apt to pick any one language over the other language number one number two is there are many language specific details there are many language specific there are many language specific details that come into play when I start writing code in any language for example let's take an example right if I start writing code in C and C++ right I'll have to use pointers extensively which are not there in other languages right or if I use Java or JavaScript I might have to use object ordered programming and use concepts called process that somebody who is comfortable in C C++ may not and may not know classes sorry somebody who no see may not know classes of course folks who know C++ would know about classes inheritance all of this stuff but we felt that these language specific details can make the code cumbersome to understand because here the whole goal of data structures and algorithms is once you know the intuition right once you know what is insertion sort you should be able to implement it any language that you are comfortable in that's why we wanted to choose a form of writing code which is language independent so we wanted to use a language independent way of explaining code to you right now while doing it of course see at the end of the day there are some common programming themes in every language every language would have FL statements right every language would have four loops every language would have while loops right every language would have variables right so similarly every language would have functions right most languages right so these are very simple construct so what we will do here is we will only use these simple ideas these very simple ideas that are present in every programming language and we will write our code in us in a very different format called a pseudo code I'll explain what the code is and some of the details about it just wait for a little while but the reason we chose not to use any specific language is to make is to avoid language specific details and make your learning language independent because today you could be writing code in C tomorrow you might write code in Python but your data structures algorithm stays the same so we would be only using very simple constructs in programming languages like these to actually show you how the code works and to do all of that we have we have what we have done is I've gone through lots of text books we have gone through lots of text books and blogs and we went through a lot of ideas on how to actually present code toward students the best source that I found was to actually use this book was to actually use this book called the introduction to algorithms right this is a very very popular book both at undergraduate and graduate level this is written by four others Carbon leiserson rivest and stein and this book is often referred to as the CLRS book this is the Wikipedia page for that book it's one of the best books that I have seen for algorithms I mean I've purchased this book almost fourteen years back when I was in my undergrad second year when I was in my undergrad second year and this book still stays with me it's it's one of the best books that I've ever read about algorithms but like every other book this book also has some shortcomings that's why we do not use just this book exclusively we use many other sources for example we use Wikipedia extensively for many of our for many of our content right so we don't stick to one book but for pseudocode to present how the algorithm works we felt one of the best pseudo codes that we've ever seen in any textbook is in the introduction to algorithms by CL Harris so we have decided to stick to the notation to the pseudocode that is available in CLRS because the pseudocode is very rigorous that is there is lot of rigor in the code and it's very very accurate very very well-written very very well thought through that's why we are going to use all of the pseudocode notation that is there in the CLRS textbook and it's very easy for you to revise also so for example if you want to see the code for insertion sort you can just google search i'm on google image search remember i'm on google image search so if you just type CLRS insertion short right you would get this very nice diagrams here right so if you just click on this this is the pseudocode for insertion sort that you will get that is there in in the textbook right forget about what the cost is what the times are we will come to this later right we'll discuss all of them later but this code snippet very crisp very clean just eight lines of beautiful code so we will use the same notation I will explain you the notation but we'll use the pseudocode extensively we will use pseudo quotes from CLRS extensively through this course right having said that lets go to the pseudocode itself right so I've just literally taken the pseudocode that we have seen in this image I've taken this and ever written it down here so that we can expand understand it here right now look look at look at what's happening here you have a function called insertion sort which is taking an array a right first thing it is taking an array a as input mouth now I will explain you some of the constructs here how this far loop could be different from for loop that you have seen in C C++ or Java so before we dive into the code let me give you some details so this for loop is here this for loop extends this for loop so this this line is inside the for loop this line is inside the for loop this line is also inside the for loop all these lines are inside the for loop so this this type of formatting so what they have done here is in C what you would typically do you would do open curly braces and a closed curly braces this is what you would do in C C++ Java right so CLRS notation for pseudocode does not use the curly braces what it does is it uses this indentation this called indentation basically what it says is if if this is my for loop anything that is a few spaces away inside this whole thing is inside the for loop this is the notation that is typically used in Python so python uses indentations like this this is called an indentation basically it is basically you have all of this at one level all of these are maybe four spaces or eight spaces away or a tab away literally right so all this so the code looks very well very clean very neat so Python uses this internally and CLRS pseudocode also uses this because it's easier and legible right number one now if you look at these two these two lines of code these two lines of code are inside the while loop so we have a while loop inside the for loop and these two lines of code are within the while loop so this is equal and to this this is equivalent to this okay there are no Seimei columns at the end like in C or C++ and this is a common so if you have something with this with two dashes here so this is a comment right so this is a comment this line is a comment and we will learn so this is simple for loop right this is simple while loop right very simple stuff this is a variable to which i am assigning a value so I will go into the details right so the moment you look at this if you know any programming language C C++ Java Python JavaScript Ruby you name it okay you'll be able to clearly understand the key structures here you have a function here you have a for loop here all these lines of code are within the for loop you have a while loop here you are declaring so you're you you're you're both declaring and assigning a value to a variable called key here you have another variable called eye right here you are assigning some value to the array right to an element in the array so a I plus 1 value you are assigning the value that is there in the key very simple constant so what are we just using we are using if-else conditions so if we are not using if-else actually we are using for while variables and simple functions that's what we'll use we will use those constructs which are common across multiple programming languages now let's go into the code itself right for any code in data structures and algorithms it is basically taking an intuition you basically take your intuition and convert it into code that's what data structures algorithm says so let's take the example here so that we can better understand it right so this is a same example that we used here this is the same example that we used here right I'm just trying to stick to the same example so that you can better connect the intuitive explanation and the code worked so first thing your J is a variable which is going from 2 to a dot length here a dot length basically means the length of the array a ok that's what it means right so a dot length here so here what is our length 8 so a dot length will be 8 okay so a dot length is equal to 8 from J equals to 2 to a dot length if you look at the intuition what what would we start in the very first iteration we took the second element we took the second element because the first element is trivially sorted we took the second element and we tried to insert it into the already sorted or trivially sorted array that's what we did in the intuition same thing we are going to do here so your J will go from your J will first be equal to two then your J is going to be equal to three see see here we are saying from J equals to two to a dot length right which means we are saying J is going to be two then three it's going to be incremented by only a value of one unless I say otherwise ready unless I say this very simple notation instead of writing for I equals four J equals to 2 J less than equal to a dot length J plus plus the C mutation right instead of all of this thing again here dot length is not valid in C I'd have to compute it and place it here so instead of this complex notation these are much more readable see the beauty of CLRS pseudocode is it's very very readable because it is language independent tries to use some of the easiest ways to do it so it says from J equals to 2 to a dot length so J will start with J equals to 2 then it will go over to J equals to 3 then 4 then 5 up to J equals to 8 okay that's what this outer loop is so this is my outer loop right then my key is going to be a J so my first key is going to be 5 because my J see let's say when J equals to 2 so when J equals to 2 what becomes 2 Mikey Mikey is going to be a of 2 which is 5 now this is a comment so this line will not be executed then what will I be my I is going to be 1 right so the first iteration I'm trying to walk you through the very first iteration now let's go to this inner loop I will call this the inner loop because this is this is a loop within a loop this is my outer loop right so it may not look what am I saying while I is greater than 0 and AI is greater than key right while a I is greater than key well these two are true you basically do this do this swapping operation or moving to the right type of operation okay here let's see here what's happening your 5c I is less when this starts right your I equals to 1.so I greater than zero yes I is greater than zero and hey I what is a I know a I will be six see what is my ai ai is going to be a 1 which is 6 and AI is greater than K yes ai is greater than K because my key is 5 right my AI is greater than my key when that happens what is it saying it's saying take your a I plus 1 so your AI plus 1 value what is AI plus 1 is nothing but a 2 it is saying make your a 2 equals 2 a 1 right what is a 1 6 so a 2 will become 6 right so what's happening to my area of my array my whole array stays the same my first element is 6 now this element 6 got moved to the right so my 6 moved here there is still 6 here right because I didn't I haven't yet changed it my three one eight seven two four everything stays the same now as soon as executed this my six got copied here my six replaced my five right then I equals to I minus one so what happened to my eye my eye became zero now if this loop again restarts because this is a loop right this is the loop now here while I is greater than zero no I is equal to zero now so I will come out of this loop as soon as I come out of this loop it says AI plus one equals two key now what is AI plus 1 it is nothing but a one equals two key a one is this this is one right this is one two three four five six seven eight so it is a one equals two key what is key five so what's happening now at the end of the first iteration at the end of my first outer iteration my first so this became five so what is my area of my array now is five six three one eight seven two four so at the end of them at the end of my first iteration so as soon as this is over again this loop starts no by my J becomes three so here my J became three as soon as Jay became three what happened my a1 and a2 are already sorted so at the end of this loop right if my J equals to two at the end of this loop right my a 1 and a 2 are sorted now again I'll start now well take again this whole thing repeats itself this whole thing repeats itself right I've shown you an example when J equals to 2 the same thing you can do when J equals to 3 keep repeating this example right what so what I want you to do to understand this code better is so I've shown you the flow of this code when J equals to do that's what I have done now on a notebook what's a at the end of J equals to do this is the this is the array that you have now take J equals to 3 because this loop is running right now try to write the same thing on your notebook so then you know how these so how this inner loop and outer loop are working in a nutshell what's happening is so so you want you want to do that walkthrough on your page on your notebook so that you understand this code better right in a nutshell let's understand what's happening in a nutshell in a nutshell this J variable right is going from the second element to the eighth element at any point as soon as suppose J equals to 3 I'm sure that the this sub array this sub array is already sorted so at any point let's assume my J equals to 6 let's assume I J equals to 6 as it keeps I iterating right my J would become equal to 6 at some point so when my J equals to 6 the situation I will be in right is is this exactly when my J equal 1 2 3 4 5 6 yes when my J equals to 6 I am trying to take the sixth element and trying to insert into this array which is already sorted right when my J equals to 6 a 1 2 a 5 are already sorted so I'm taking the value of a 6 or AJ into key right and I will start from a J minus 1 from here from here rest of the elements this is exactly what the intuition says right what does intuition say I take this element I compare it with each of these elements right and keep moving these elements to the right if they are greater than the sell gator than the key that I have that's exactly what I'm doing so what i'm doing here is when j equals to 6 i'm taking from J equals from 5 up to 0 that's what this is this is saying my I equals to 5 up to 0 so I'm going to go from 5 to 0 and when I am going from 5 to 0 I'll keep moving these elements to the right if they are greater than my key if they're greater than my key I'm going to move it to the right that's what I'm doing we have seen this with J equals to 2 same thing is happening at the end wherever the key element should be placed I'll place it very very simple code to understand but I strongly recommend you to actually repeat see I've done this simple exercise with J equals to 2 right I want you to do it with J equals to 3 J equals to 4 or 5 so that you understand this code better if you are very good at programming you would have understood it by now it's like if you are comfortable with any programming language this is straightforward right these are simply 2 loops you have an outer loop and an inner loop that's it very straightforward code nothing fancy nothing complicated hereso in the previous video we understood the intuition behind insertion sort and we took a very simple example like this to understand what actually is happening insertion sort right so now let's try to move to some code right let's actually write down the code of course as important as intuition is equally important is the ability to convert that intuition into code now here we had a cup we had a few choices we could have written this whole code in any major programming language we could have written it didn't see C++ Java Python right or more web based languages like JavaScript or Ruby we would have written the code in any of these programming languages but we decided not to write so we will actually understand all of the code using a pseudo code now the pseudo in English the word pseudo basically means false so I'll explain you what the pseudo code is and why we are choosing it but the reason why we do not use any specific language is because some of our students might be good at C right if I so if I chose C some of our students might might have learned whole of programming from C some students may not know C but they might know Python or there could be some people who are web developers who know JavaScript who don't know other languages very well so because we have wide spectrum of students from various backgrounds we felt it is not apt to pick any one language over the other language number one number two is there are many language specific details there are many language specific there are many language specific details that come into play when I start writing code in any language for example let's take an example right if I start writing code in C and C++ right I'll have to use pointers extensively which are not there in other languages right or if I use Java or JavaScript I might have to use object ordered programming and use concepts called process that somebody who is comfortable in C C++ may not and may not know classes sorry somebody who no see may not know classes of course folks who know C++ would know about classes inheritance all of this stuff but we felt that these language specific details can make the code cumbersome to understand because here the whole goal of data structures and algorithms is once you know the intuition right once you know what is insertion sort you should be able to implement it any language that you are comfortable in that's why we wanted to choose a form of writing code which is language independent so we wanted to use a language independent way of explaining code to you right now while doing it of course see at the end of the day there are some common programming themes in every language every language would have FL statements right every language would have four loops every language would have while loops right every language would have variables right so similarly every language would have functions right most languages right so these are very simple construct so what we will do here is we will only use these simple ideas these very simple ideas that are present in every programming language and we will write our code in us in a very different format called a pseudo code I'll explain what the code is and some of the details about it just wait for a little while but the reason we chose not to use any specific language is to make is to avoid language specific details and make your learning language independent because today you could be writing code in C tomorrow you might write code in Python but your data structures algorithm stays the same so we would be only using very simple constructs in programming languages like these to actually show you how the code works and to do all of that we have we have what we have done is I've gone through lots of text books we have gone through lots of text books and blogs and we went through a lot of ideas on how to actually present code toward students the best source that I found was to actually use this book was to actually use this book called the introduction to algorithms right this is a very very popular book both at undergraduate and graduate level this is written by four others Carbon leiserson rivest and stein and this book is often referred to as the CLRS book this is the Wikipedia page for that book it's one of the best books that I have seen for algorithms I mean I've purchased this book almost fourteen years back when I was in my undergrad second year when I was in my undergrad second year and this book still stays with me it's it's one of the best books that I've ever read about algorithms but like every other book this book also has some shortcomings that's why we do not use just this book exclusively we use many other sources for example we use Wikipedia extensively for many of our for many of our content right so we don't stick to one book but for pseudocode to present how the algorithm works we felt one of the best pseudo codes that we've ever seen in any textbook is in the introduction to algorithms by CL Harris so we have decided to stick to the notation to the pseudocode that is available in CLRS because the pseudocode is very rigorous that is there is lot of rigor in the code and it's very very accurate very very well-written very very well thought through that's why we are going to use all of the pseudocode notation that is there in the CLRS textbook and it's very easy for you to revise also so for example if you want to see the code for insertion sort you can just google search i'm on google image search remember i'm on google image search so if you just type CLRS insertion short right you would get this very nice diagrams here right so if you just click on this this is the pseudocode for insertion sort that you will get that is there in in the textbook right forget about what the cost is what the times are we will come to this later right we'll discuss all of them later but this code snippet very crisp very clean just eight lines of beautiful code so we will use the same notation I will explain you the notation but we'll use the pseudocode extensively we will use pseudo quotes from CLRS extensively through this course right having said that lets go to the pseudocode itself right so I've just literally taken the pseudocode that we have seen in this image I've taken this and ever written it down here so that we can expand understand it here right now look look at look at what's happening here you have a function called insertion sort which is taking an array a right first thing it is taking an array a as input mouth now I will explain you some of the constructs here how this far loop could be different from for loop that you have seen in C C++ or Java so before we dive into the code let me give you some details so this for loop is here this for loop extends this for loop so this this line is inside the for loop this line is inside the for loop this line is also inside the for loop all these lines are inside the for loop so this this type of formatting so what they have done here is in C what you would typically do you would do open curly braces and a closed curly braces this is what you would do in C C++ Java right so CLRS notation for pseudocode does not use the curly braces what it does is it uses this indentation this called indentation basically what it says is if if this is my for loop anything that is a few spaces away inside this whole thing is inside the for loop this is the notation that is typically used in Python so python uses indentations like this this is called an indentation basically it is basically you have all of this at one level all of these are maybe four spaces or eight spaces away or a tab away literally right so all this so the code looks very well very clean very neat so Python uses this internally and CLRS pseudocode also uses this because it's easier and legible right number one now if you look at these two these two lines of code these two lines of code are inside the while loop so we have a while loop inside the for loop and these two lines of code are within the while loop so this is equal and to this this is equivalent to this okay there are no Seimei columns at the end like in C or C++ and this is a common so if you have something with this with two dashes here so this is a comment right so this is a comment this line is a comment and we will learn so this is simple for loop right this is simple while loop right very simple stuff this is a variable to which i am assigning a value so I will go into the details right so the moment you look at this if you know any programming language C C++ Java Python JavaScript Ruby you name it okay you'll be able to clearly understand the key structures here you have a function here you have a for loop here all these lines of code are within the for loop you have a while loop here you are declaring so you're you you're you're both declaring and assigning a value to a variable called key here you have another variable called eye right here you are assigning some value to the array right to an element in the array so a I plus 1 value you are assigning the value that is there in the key very simple constant so what are we just using we are using if-else conditions so if we are not using if-else actually we are using for while variables and simple functions that's what we'll use we will use those constructs which are common across multiple programming languages now let's go into the code itself right for any code in data structures and algorithms it is basically taking an intuition you basically take your intuition and convert it into code that's what data structures algorithm says so let's take the example here so that we can better understand it right so this is a same example that we used here this is the same example that we used here right I'm just trying to stick to the same example so that you can better connect the intuitive explanation and the code worked so first thing your J is a variable which is going from 2 to a dot length here a dot length basically means the length of the array a ok that's what it means right so a dot length here so here what is our length 8 so a dot length will be 8 okay so a dot length is equal to 8 from J equals to 2 to a dot length if you look at the intuition what what would we start in the very first iteration we took the second element we took the second element because the first element is trivially sorted we took the second element and we tried to insert it into the already sorted or trivially sorted array that's what we did in the intuition same thing we are going to do here so your J will go from your J will first be equal to two then your J is going to be equal to three see see here we are saying from J equals to two to a dot length right which means we are saying J is going to be two then three it's going to be incremented by only a value of one unless I say otherwise ready unless I say this very simple notation instead of writing for I equals four J equals to 2 J less than equal to a dot length J plus plus the C mutation right instead of all of this thing again here dot length is not valid in C I'd have to compute it and place it here so instead of this complex notation these are much more readable see the beauty of CLRS pseudocode is it's very very readable because it is language independent tries to use some of the easiest ways to do it so it says from J equals to 2 to a dot length so J will start with J equals to 2 then it will go over to J equals to 3 then 4 then 5 up to J equals to 8 okay that's what this outer loop is so this is my outer loop right then my key is going to be a J so my first key is going to be 5 because my J see let's say when J equals to 2 so when J equals to 2 what becomes 2 Mikey Mikey is going to be a of 2 which is 5 now this is a comment so this line will not be executed then what will I be my I is going to be 1 right so the first iteration I'm trying to walk you through the very first iteration now let's go to this inner loop I will call this the inner loop because this is this is a loop within a loop this is my outer loop right so it may not look what am I saying while I is greater than 0 and AI is greater than key right while a I is greater than key well these two are true you basically do this do this swapping operation or moving to the right type of operation okay here let's see here what's happening your 5c I is less when this starts right your I equals to 1.so I greater than zero yes I is greater than zero and hey I what is a I know a I will be six see what is my ai ai is going to be a 1 which is 6 and AI is greater than K yes ai is greater than K because my key is 5 right my AI is greater than my key when that happens what is it saying it's saying take your a I plus 1 so your AI plus 1 value what is AI plus 1 is nothing but a 2 it is saying make your a 2 equals 2 a 1 right what is a 1 6 so a 2 will become 6 right so what's happening to my area of my array my whole array stays the same my first element is 6 now this element 6 got moved to the right so my 6 moved here there is still 6 here right because I didn't I haven't yet changed it my three one eight seven two four everything stays the same now as soon as executed this my six got copied here my six replaced my five right then I equals to I minus one so what happened to my eye my eye became zero now if this loop again restarts because this is a loop right this is the loop now here while I is greater than zero no I is equal to zero now so I will come out of this loop as soon as I come out of this loop it says AI plus one equals two key now what is AI plus 1 it is nothing but a one equals two key a one is this this is one right this is one two three four five six seven eight so it is a one equals two key what is key five so what's happening now at the end of the first iteration at the end of my first outer iteration my first so this became five so what is my area of my array now is five six three one eight seven two four so at the end of them at the end of my first iteration so as soon as this is over again this loop starts no by my J becomes three so here my J became three as soon as Jay became three what happened my a1 and a2 are already sorted so at the end of this loop right if my J equals to two at the end of this loop right my a 1 and a 2 are sorted now again I'll start now well take again this whole thing repeats itself this whole thing repeats itself right I've shown you an example when J equals to 2 the same thing you can do when J equals to 3 keep repeating this example right what so what I want you to do to understand this code better is so I've shown you the flow of this code when J equals to do that's what I have done now on a notebook what's a at the end of J equals to do this is the this is the array that you have now take J equals to 3 because this loop is running right now try to write the same thing on your notebook so then you know how these so how this inner loop and outer loop are working in a nutshell what's happening is so so you want you want to do that walkthrough on your page on your notebook so that you understand this code better right in a nutshell let's understand what's happening in a nutshell in a nutshell this J variable right is going from the second element to the eighth element at any point as soon as suppose J equals to 3 I'm sure that the this sub array this sub array is already sorted so at any point let's assume my J equals to 6 let's assume I J equals to 6 as it keeps I iterating right my J would become equal to 6 at some point so when my J equals to 6 the situation I will be in right is is this exactly when my J equal 1 2 3 4 5 6 yes when my J equals to 6 I am trying to take the sixth element and trying to insert into this array which is already sorted right when my J equals to 6 a 1 2 a 5 are already sorted so I'm taking the value of a 6 or AJ into key right and I will start from a J minus 1 from here from here rest of the elements this is exactly what the intuition says right what does intuition say I take this element I compare it with each of these elements right and keep moving these elements to the right if they are greater than the sell gator than the key that I have that's exactly what I'm doing so what i'm doing here is when j equals to 6 i'm taking from J equals from 5 up to 0 that's what this is this is saying my I equals to 5 up to 0 so I'm going to go from 5 to 0 and when I am going from 5 to 0 I'll keep moving these elements to the right if they are greater than my key if they're greater than my key I'm going to move it to the right that's what I'm doing we have seen this with J equals to 2 same thing is happening at the end wherever the key element should be placed I'll place it very very simple code to understand but I strongly recommend you to actually repeat see I've done this simple exercise with J equals to 2 right I want you to do it with J equals to 3 J equals to 4 or 5 so that you understand this code better if you are very good at programming you would have understood it by now it's like if you are comfortable with any programming language this is straightforward right these are simply 2 loops you have an outer loop and an inner loop that's it very straightforward code nothing fancy nothing complicated here\n"