-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.js
More file actions
106 lines (94 loc) · 3.16 KB
/
quickSort.js
File metadata and controls
106 lines (94 loc) · 3.16 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {
numberArray,
syncTimeout,
clearTopContainer,
color1,
color2,
drawBarEnd,
} from "./script.js";
/* Quick Sort
Der Quick sort ist ein schneller (O(nlogn)), rekursiver Sortieralgorithmus nach dem divide an conquer Prinzip
Zunächst wird die Liste in 2 Teillisten getrennt. Dazu wird ein Pivotelement (hier ist es das erste Element) ausgewählt und alle
anderen Elemente werden nach diesem Pivotelement sortiert. Das heißt, am Ende stehen alle Elemente die kleiner als das Pivot Element sind auf
der linken Seite des PivotElements und alle anderen auf der rechten Seite des Pivotelements. Anschließend erfolgt die Rekusion. Diese Schritte
werden zunächst mit der linken Teilliste (= alle Elemente links des Pivotelements) und dann mit der rechten Teilliste wiederholt.
Die Teilliste die aktuell bearbeitet wird, wird gelb markeirt.
Das aktuelle PivotElement ist blau hinterlegt.
Das Element, dass gerade mit dem PivotElement verglichen wird, ist grün gefärbt, sofern es am richtigen Platz ist, wenn nicht färbt es sich zunächst rot
und nach erfolgreichem Tausch grün.
*/
export async function buttonQuickSort() {
await quickSort(numberArray, 0, numberArray.length - 1);
clearTopContainer();
drawBarEnd();
}
async function quickSort(arr, start, end) {
if (start >= end) {
return;
}
let index = await partition(arr, start, end);
clearTopContainer();
drawBarQuickSort(index);
await Promise.all([
// rekursion
quickSort(arr, start, index - 1),
quickSort(arr, index + 1, end),
]);
}
async function partition(arr, start, end) {
let pivotValue = arr[end];
let pivotIndex = start;
// mark region
clearTopContainer();
drawBarQuickSort(pivotIndex, start, end);
await syncTimeout();
// pivot element überspringen
for (let i = start + 1; i < end; i++) {
if (arr[i] < pivotValue) {
clearTopContainer();
drawBarQuickSort(pivotIndex, start, end, i, false);
await syncTimeout();
swap(arr, i, pivotIndex);
let indexSwappedElement = pivotIndex;
pivotIndex++;
clearTopContainer();
drawBarQuickSort(pivotIndex, start, end, indexSwappedElement, true);
await syncTimeout();
} else {
clearTopContainer();
drawBarQuickSort(pivotIndex, start, end, i, true);
await syncTimeout();
}
}
swap(arr, pivotIndex, end);
clearTopContainer(pivotIndex);
drawBarQuickSort(pivotIndex);
await syncTimeout();
return pivotIndex;
}
function drawBarQuickSort(pivot, start, end, selected, bool) {
for (let b = 0; b < numberArray.length; b++) {
var para = document.createElement("div");
para.className = "bar";
if (b >= start && b <= end) {
para.style.backgroundColor = "yellow";
}
if (b == pivot) {
console.log("asdf");
para.style.backgroundColor = "blue";
}
if (b == selected && bool == false) {
para.style.backgroundColor = "red";
}
if (b == selected && bool == true) {
para.style.backgroundColor = "green";
}
para.style.height = numberArray[b] / 5 + "%";
document.getElementById("topContainer").appendChild(para);
}
}
function swap(arr, a, b) {
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}