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: merge-sort/index.js
+5-4Lines changed: 5 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,11 @@
2
2
3
3
/*
4
4
1. Use recursion.
5
-
2. If the length of the array is less than 2, return the array.
6
-
3. Use Math.floor() to calculate the middle point of the array.
7
-
4. Use Array.prototype.slice() to slice the array in two and recursively call mergeSort() on the created subarrays.
8
-
5. Finally, use Array.from() and Array.prototype.shift() to combine the two sorted subarrays into one.
5
+
2. Use the spread operator (...) to clone the original array, arr.
6
+
3. If the length of the array is less than 2, return the cloned array.
7
+
4. Use Math.floor() to calculate the index of the pivot element.
8
+
5. Use Array.prototype.reduce() and Array.prototype.push() to split the array into two subarrays. The first one contains elements smaller than or equal to pivot and the second on elements greather than it. Destructure the result into two arrays.
9
+
6. Recursively call quickSort() on the created subarrays.
Copy file name to clipboardExpand all lines: quick-sort/index.js
+11Lines changed: 11 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,14 @@
1
+
// Sorts an array of numbers, using the quicksort algorithm.
2
+
3
+
/*
4
+
1. Use recursion.
5
+
2. Use the spread operator (...) to clone the original array, arr.
6
+
3. If the length of the array is less than 2, return the cloned array.
7
+
4. Use Math.floor() to calculate the index of the pivot element.
8
+
5. Use Array.prototype.reduce() and Array.prototype.push() to split the array into two subarrays. The first one contains elements smaller than or equal to pivot and the second on elements greather than it. Destructure the result into two arrays.
9
+
6. Recursively call quickSort() on the created subarrays.
0 commit comments