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
Copy file name to clipboardExpand all lines: Sorting/bubble-sort-basic-FUTURE-REFERENCE.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ swap = (arr, i, j) => {
9
9
arr[j]=temp;
10
10
}
11
11
12
-
// Necessary warning :) :) - this is a super basic implementation to understand the principle behind bubble sort (going through all comparisons) but there's huge room for performance improvement on this.
12
+
// SOLUTION-1 - Necessary warning :) :) - this is a super basic implementation to understand the principle behind bubble sort (going through all comparisons) but there's huge room for performance improvement on this.
Copy file name to clipboardExpand all lines: Sorting/insertion-sort-with-splice.js
+44-8Lines changed: 44 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -14,17 +14,41 @@ insertionSort = arr => {
14
14
letarray=arr.slice(0);
15
15
16
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]
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
+
20
+
C> Note, < arr.splice(j, 0, 'item') > means that at j-th position I am placing the element 'item' while removing zero element from the array.
21
+
22
+
AND remember array.splice() MUTATES THE ORIGINAL ARRAY AND RETURN THAT MUTATED ARRAY
23
+
24
+
D> And < arr.splice(i, 1)[0] > will delete 1 element at i-th position and return an array containing deleted element. Hence I am getting that deleted element by using 0-index position with [0]
25
+
26
+
This is how the splice is working here -
27
+
28
+
Pick-out (or return) the deleted element from the given array
29
+
30
+
let myArr1 = [ 2, 1, 3, 4]
31
+
32
+
console.log(myArr1.splice(1, 1)[0]); // => 1
33
+
34
+
Then replace the 0-index element with the third-argument in the .splice() method
Copy file name to clipboardExpand all lines: Sorting/mergeSort-Basic-Recursive-FUTURE-REFERENCE.js
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
// ROHAN'S NOTE IS TO REFER HERE IN FUTURE - Implementation of merge-sort algorithm, to sort an array of numbers in O(N×log(N)) time.
1
+
// ROHAN'S NOTE HERE FOR FUTURE REFERENCE - Implementation of merge-sort algorithm, to sort an array of numbers in O(N×log(N)) time.
2
2
3
3
/* https://www.geeksforgeeks.org/merge-sort/
4
4
5
-
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following pseudo-code.
5
+
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The helper function merge2SortedArrays() is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following pseudo-code.
6
6
7
7
MergeSort(arr[], l, r)
8
8
If r > l
@@ -24,7 +24,7 @@ A> The array is recursively divided in two halves till the size becomes 1.
24
24
25
25
B> Once the size becomes 1, the merge processes comes into action, meaning I invoke the merge function on the 2 single-element arrays. That is, take adjacent pairs of two singleton lists, sort the elements of the 2 list, and merge them to form a list (ie array) of 2 elements .
26
26
27
-
C> So the merge() function below is a completely independent function and will just take 2 sorted-arrays ( the individual arrays MUST BE SORTED ), and then concat the elements to forma a single SORTED array. That is, within the merge() function before pushing the elements to the final result array (which will be the output from merge ) it checks for proper-sorting between the 2 elements on the 2 arrays.
27
+
C> So the merge2SortedArrays() function below is a completely independent function and will just take 2 sorted-arrays ( the individual arrays MUST BE SORTED ), and then concat the elements to forma a single SORTED array. That is, within the merge() function before pushing the elements to the final result array (which will be the output from merge ) it checks for proper-sorting between the 2 elements on the 2 arrays.
28
28
29
29
merge( [1, 3, 5, 7 ] , [ 2, 4, 6, 8] ) >> will produce the below
0 commit comments