Skip to content

Commit 926cd5b

Browse files
committed
Completed Quick Sort function, updated ReadMe.md
1 parent 69334c9 commit 926cd5b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ A collection of Javascript design patterns and common algorithms.
2828
- [ ] Find Identical Node
2929
- [x] Flatten Array
3030
- [x] Insertion Sort
31-
- [ ] Merge Sort
32-
- [ ] Quick Sort
31+
- [x] Merge Sort
32+
- [x] Quick Sort
3333

3434

3535
## Contributors

algorithms/QuickSort.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function quickSort(arr, left, right) {
2+
let len = arr.length,
3+
pivot,
4+
partitionIndex;
5+
6+
if(left < right) {
7+
pivot = right;
8+
partitionIndex = partition(arr, pivot, left, right);
9+
quickSort(arr, left, partitionIndex - 1);
10+
quickSort(arr, partitionIndex + 1, right);
11+
}
12+
return arr;
13+
}
14+
15+
function partition(arr, pivot, left, right) {
16+
let pivotValue = arr[pivot],
17+
partitionIndex = left;
18+
19+
for(let i = left; i < right; i++) {
20+
if(arr[i] < pivotValue) {
21+
swap(arr, i, partitionIndex);
22+
partitionIndex++;
23+
}
24+
}
25+
swap(arr, right, partitionIndex);
26+
return partitionIndex;
27+
}
28+
29+
function swap(arr, i, j) {
30+
let temp = arr[i];
31+
arr[i] = arr[j];
32+
arr[j] = temp;
33+
}

0 commit comments

Comments
 (0)