Reading notes about code for my future reference.
Hi all my name is Zoe Gonzalez, and welcome to my blog today we will be going the process of a step by step evaluation from pseudo code to Javascript.
InsertionSort(int[] arr) ### JavaScript
function insertionSort(arr, n)
First we start with an insertion Sort function declaration and then we move on.
FOR i = 1 to arr.length
int j <-- i - 1
int temp <-- arr[i]
let i, a, j;
for(i = 1; i < n; i++)
The next step is to let I, the second variable and declaration of a temp j value for the nested loop. We will use this as a condition to stop the loop.
arr[j + 1] <-- arr[j]
j <-- j - 1
while (j >= 0 && arr [j] > a)
{
arr[j+1] = arr[j];
j = j - 1;
}
The second part is a nested loop but this loop is a while loop here is where we determine the location of the sorted list. It compares the left and right values to approriately determine where to place it.
arr[j + 1] <-- temp
arr[j +1] = a;
here is where we reassign the sorted array and essentially create the new sorted array.