Skip to content

Commit 7c01b95

Browse files
authored
Create quickSort.js
1 parent fa84e4c commit 7c01b95

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

quickSort.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function partition(arr, start, end){
2+
// Taking the last element as the pivot
3+
const pivotValue = arr[end];
4+
let pivotIndex = start;
5+
for (let i = start; i < end; i++) {
6+
if (arr[i] < pivotValue) {
7+
// Swapping elements
8+
[arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]];
9+
// Moving to next element
10+
pivotIndex++;
11+
}
12+
}
13+
14+
// Putting the pivot value in the middle
15+
[arr[pivotIndex], arr[end]] = [arr[end], arr[pivotIndex]]
16+
return pivotIndex;
17+
};

0 commit comments

Comments
 (0)