-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathThreeWayQuickSort.html
More file actions
41 lines (37 loc) · 933 Bytes
/
ThreeWayQuickSort.html
File metadata and controls
41 lines (37 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="SortTestHelper.js"></script>
</head>
<body>
<script>
function threeWayQuickSort(arr) {
let len = arr.length
_partition(arr, 0, len - 1)
console.log(arr)
}
function _partition(arr, l, r) {
if (l >= r) return
let lt = l,
gt = r + 1,
v = arr[l],
i = l + 1;
while (i < gt) {
if (arr[i] < v) {
swap(arr, ++lt, i++)
} else if (arr[i] === v) {
i++
} else {
swap(arr, --gt, i)
}
}
swap(arr, lt, l);
_partition(arr, l, lt - 1);
_partition(arr, gt, r)
}
threeWayQuickSort([32, 153, 100, -50, -10, 6, 5, 1356, 20, 160, 2, 1432, 4, 50, 14, 102, -30, 3, 45, 1312, 1, -1, -3])
</script>
</body>
</html>