-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.html
More file actions
139 lines (125 loc) · 4.1 KB
/
quicksort.html
File metadata and controls
139 lines (125 loc) · 4.1 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
<title>QuickSort</title>
</head>
<body>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"
integrity="sha512-N4kV7GkNv7QR7RX9YF/olywyIgIwNvfEe2nZtfyj73HdjCUkAfOBDbcuJ/cTaN04JKRnw1YG1wnUyNKMsNgg3g=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<script>
const window_w = 550;
var array_size;
const fps = 60;
const asc = true;
var swapDelay;
var reset;
var numbers;
function setup() {
createCanvas(window_w, window_w);
array_size = createSlider(10, 1000, 300, 1).position(20, 20);
swapDelay = createSlider(5, 50, 5, 1).position(20, 40);
reset = createButton("Reset")
.position(0, window_w + 20)
.style("width", "200px")
.style("height", "25px")
.mousePressed(() => {
reset.hide();
numbers = new Array(array_size.value());
for (let i = 0; i < numbers.length; i++) {
numbers[i] = Math.random() * (window_w - 1) + 1;
}
quickSort(numbers, 0, numbers.length - 1);
loop();
})
.hide();
numbers = new Array(array_size.value());
for (let i = 0; i < numbers.length; i++) {
numbers[i] = Math.random() * (window_w - 1) + 1;
}
frameRate(fps);
quickSort(numbers, 0, numbers.length - 1);
}
async function quickSort(arr, start, end) {
//End one step if start and end cross over each other
if (start >= end) return;
//Sort array according to pivot and split at new pivot index into two arrays
let pivot = arr[end];
let pivot_index = start;
for (let i = start; i < end; i++) {
if (asc) {
if (arr[i] > pivot) {
//wait for swapping i and pivot index
await swap(arr, i, pivot_index);
pivot_index++;
}
} else {
if (arr[i] < pivot) {
//wait for swapping i and pivot index
await swap(arr, i, pivot_index);
pivot_index++;
}
}
}
//wait for swapping pivot index with end
await swap(arr, pivot_index, end);
//wait for left and right half to be sorted
await Promise.all([
quickSort(arr, start, pivot_index - 1),
quickSort(arr, pivot_index + 1, end),
]);
}
function draw() {
background(0, 0, 0);
//Draw array
for (let i = 0; i < numbers.length; i++) {
//Line properties
let thickness = window_w / numbers.length;
let xpos = thickness * i;
let ypos = window_w;
stroke("#88ff00");
strokeWeight(thickness);
line(xpos + thickness / 2, ypos, xpos + thickness / 2, numbers[i]);
//Dont check every frame but every 10th frame
if (frameCount % 10 === 0) {
if (isSorted(numbers, asc)) {
reset.show();
noLoop();
}
}
}
}
function isSorted(arr, asc) {
for (let i = 0; i < arr.length - 1; i++) {
if (asc) {
if (arr[i] < arr[i + 1]) return false;
} else {
if (arr[i] > arr[i + 1]) return false;
}
}
return true;
}
async function swap(array, index1, index2) {
await sleep(swapDelay.value());
let temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
</script>
</body>
</html>