class QuickSortMultiThreading {
constructor(start, end, arr) {
this
.start = start;
this
.end = end;
this
.arr = arr;
}
partition(start, end, arr) {
let i = start;
let j = end;
const pivoted = Math.floor(Math.random() * (j - i + 1)) + i;
[arr[j], arr[pivoted]] = [arr[pivoted], arr[j]];
j--;
while
(i <= j) {
if
(arr[i] <= arr[end]) {
i++;
continue
;
}
if
(arr[j] >= arr[end]) {
j--;
continue
;
}
[arr[j], arr[i]] = [arr[i], arr[j]];
j--;
i++;
}
[arr[j + 1], arr[end]] = [arr[end], arr[j + 1]];
return
j + 1;
}
operator() {
if
(
this
.start >=
this
.end) {
return
;
}
const p =
this
.partition(
this
.start,
this
.end,
this
.arr);
const left =
new
QuickSortMultiThreading(
this
.start, p - 1,
this
.arr);
const right =
new
QuickSortMultiThreading(p + 1,
this
.end,
this
.arr);
const numThreads = 2;
const promises = [
new
Promise(resolve => resolve(left.compute())),
new
Promise(resolve => resolve(right.compute()))
];
return
Promise.all(promises);
}
}
const n = 7;
const arr = [54, 64, 95, 82, 12, 32, 63];
const mainInstance =
new
QuickSortMultiThreading(0, n - 1, arr);
await mainInstance.operator();
console.log(arr.join(
' '
));