You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A) Loop from i = 1 (second element of the array) to n-1.
4
+
B) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]
5
+
C) Say I get 12, 11 as the second and third element. Since 11 is smaller than 12, move 12 and insert 11 before 12
6
+
*/
7
+
8
+
insertionSort=arr=>{
9
+
10
+
if(!Array.isArray(arr)||arr.length<2){
11
+
returnarr;
12
+
}
13
+
14
+
letarray=arr.slice(0);
15
+
16
+
17
+
/* A> Here each time I find aff[i] < arr[j] I effectively have to swap the position of i and j.
18
+
B> But instead of using a swap function, I returned a spliced array where the j-th position is replaced with i-th position.
19
+
C> Note, < arr.splice(j, 0, 'item') > means that at j-th position I am placing the 'item' and removing zero element.
20
+
D> And < arr.splice(i, 1)[0] > will delete 1 element from i-th position and return and return an array containing deleted element. Hence I am getting that delete element by accessing [0]
Copy file name to clipboardExpand all lines: Sorting/insertion_sort-with-swap.js
+6-3Lines changed: 6 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -24,9 +24,12 @@ function insertion_sort(array) {
24
24
vari,j;
25
25
26
26
/*
27
-
1. Assume first element is sorted
28
-
2. Add first unsorted element to sorted array
29
-
3. Compare new element in sorted array with previous elements to determine correct destination index in sorted array
27
+
1. Assume first element is sorted. So, Loop from i = 1 (second element of the array) to n-1.
28
+
29
+
2. Add first unsorted element to sorted array.
30
+
31
+
3. Compare new element in sorted array with previous elements to determine correct destination index in sorted array. Say I get 12, 11 as the second and third element. Since 11 is smaller than 12, move 12 and insert 11 before 12
32
+
30
33
4. In the below, at the last step, I had to decrement j by one, to stop the while loop. Because, with the decremented value of j, in the next iteration of the loop, one of the while loop condition will fail
/* Explanation of < Math.floor(Math.random() * (max - min + 1)) + min >
56
-
57
-
By adding 1, I am making the maximum inclusive ( the minimum is inclusive anyway). Because, the Math.random() function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1. the formula can generate the correct amount of numbers but they always start at 0 because the range from Math.random starts from 0. https://teamtreehouse.com/community/need-an-explanation
58
-
59
-
Math.random() returns a number from 0 to 1, not including one. Flooring this number will always result in a zero.
60
-
61
-
If you multiply this float by the max prior to flooring, you will get numbers all the way up to the maximum, including those below the minimum, but not the maximum itself. Say you have 10 as the maximum, you cannot get a 10.
62
-
63
-
In order to rule out those below the minimum, you subtract the minimum from the maximum. In order to include the maximum, you add one. This is all multiplied by the random floating point number between 0 and 1.
64
-
65
-
At that point, we aren't quite there. You will be receiving numbers based on the range between the minimum and maximum as explained above. Adding the minimum back in, post-randomizing, ensure that you have numbers that are between the minimum and the maximum.
66
-
67
-
TLDR: The range of numbers (i.e. the number of numbers between min and max) is defined by those within the floor method's parentheses. The minimum added at the end ensures these numbers are between the desired minimum and maximum.
68
-
69
-
By subtracting the minimum number from the maximum, you define the range. Say you have minimum of 5 and a maximum of 20, you will get the range of 15. There are 15 possible numbers that you could select at random between these bounds.
70
-
71
-
Paul-Note - So, if I dont add Min of 5, the formulae will give me all the random numbers between 0 and 15. But I want between 5 and 20. So thats why I add back the min.
72
-
73
-
Another great visual explanation at - https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range
// Returns a random integer between min (inclusive) and max (inclusive). Using Math.round() will give a non-uniform distribution, which we dont want in this case.
0 commit comments