-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDouble Way Quick Sort.html
More file actions
49 lines (45 loc) · 1.01 KB
/
Copy pathDouble Way Quick Sort.html
File metadata and controls
49 lines (45 loc) · 1.01 KB
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
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="SortTestHelper.js"></script>
</head>
<body>
<script>
function DoubleWayQuickSort(arr) {
let len = arr.length
_quickSort(arr, 0, len - 1)
console.log(arr)
}
function _quickSort(arr, l, r) {
if (l >= r) {
return
}
let p = _partition(arr, l, r);
_quickSort(arr, l, p - 1);
_quickSort(arr, p + 1, r)
}
function _partition(arr, l, r) {
let v = arr[l],
i = l + 1,
j = r;
while (true) {
while (i <= r && arr[i] < v) {
i++
}
while (j >= l && arr[j] > v) {
j--
}
if(i > j) break;
swap(arr, i, j);
i++;
j--;
}
swap(arr, l, j);
return j
}
DoubleWayQuickSort([3, 1, 3, 5, 4, 4, 4, 7, 2, 4, 9, 4, 4, 6, 10, 8])
</script>
</body>
</html>