Skip to content

Commit 6395338

Browse files
committed
Update quicksort.
1 parent 500ba6f commit 6395338

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

merge-sort/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
/*
44
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.
910
*/
1011

1112
const mergeSort = arr => {

quick-sort/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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.
10+
*/
11+
112
const quickSort = arr => {
213
const a = [...arr];
314
if (a.length < 2) return a;

0 commit comments

Comments
 (0)