-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.html
More file actions
192 lines (170 loc) · 7.28 KB
/
Copy pathquick_sort.html
File metadata and controls
192 lines (170 loc) · 7.28 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quick Sort - Interactive Visualization</title>
<meta name="description" content="Picks a random pivot, partitions everything smaller to its left, then sorts each side independently.">
<link rel="stylesheet" href="../assets/visualizer.css">
<script>
// Apply the saved theme before first paint, so the page never flashes
// the wrong one on load.
try {
var savedTheme = localStorage.getItem('sorting-algorithms-theme');
if (savedTheme) document.documentElement.setAttribute('data-theme', savedTheme);
} catch (error) { /* storage unavailable - fall back to the OS preference */ }
</script>
</head>
<body>
<div class="container">
<header class="page-header">
<div>
<h1>Quick Sort</h1>
<p class="subtitle">Picks a random pivot, partitions everything smaller to its left, then sorts each side independently.</p>
</div>
<div class="header-actions">
<a class="back-link" href="../index.html">All algorithms</a>
<button class="theme-toggle" id="themeToggle" type="button" aria-label="Switch colour theme">
<svg class="icon-moon" viewBox="0 0 24 24" aria-hidden="true">
<path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"/>
</svg>
<svg class="icon-sun" viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="4"/>
<path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/>
</svg>
</button>
</div>
</header>
<div class="panel">
<div class="buttons">
<button id="startBtn" type="button">Start Animation</button>
<button id="generateBtn" type="button">Generate New Example</button>
<button id="speedBtn" type="button">Speed: Slow (x1)</button>
</div>
</div>
<div class="array-display">
<div class="array-container" id="arrayContainer"></div>
<div class="aux-panel" id="auxPanel">
<div class="aux-title" id="auxTitle"></div>
<div class="aux-row" id="auxRow"></div>
</div>
</div>
<div class="steps" id="steps" role="status" aria-live="polite"></div>
<div class="panel">
<h2>Animation State</h2>
<div class="status-info">
<div class="info-item">
<div class="info-label">Current depth</div>
<div class="info-value" id="depth">0</div>
</div>
<div class="info-item">
<div class="info-label">Comparisons</div>
<div class="info-value" id="comparisons">0</div>
</div>
<div class="info-item">
<div class="info-label">Swaps</div>
<div class="info-value" id="swaps">0</div>
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-swatch" style="background:linear-gradient(135deg,#f472b6,#db2777)"></span>Pivot
</div>
<div class="legend-item">
<span class="legend-swatch" style="background:linear-gradient(135deg,#fbbf24,#d97706)"></span>Comparing
</div>
<div class="legend-item">
<span class="legend-swatch" style="background:linear-gradient(135deg,#ef4444,#dc2626)"></span>Swapping
</div>
<div class="legend-item">
<span class="legend-swatch" style="background:linear-gradient(135deg,#10b981,#059669)"></span>Final position
</div>
</div>
</div>
<footer class="page-footer">
Created by <strong>Amine</strong> ·
<a href="https://github.com/amineTNYT" target="_blank" rel="noopener">@amineTNYT</a>
· <a href="./README.md">Notes on this algorithm</a>
</footer>
</div>
<script src="../assets/visualizer.js"></script>
<script>
SortingVisualizer.create({
defaultExample: [7, 2, 9, 4, 1, 8, 3],
randomLength: 8,
stats: [{ id: 'depth' }, { id: 'comparisons' }, { id: 'swaps' }],
run: async function (ctx) {
async function partition(low, high) {
// A random pivot is what keeps sorted input from degenerating to O(n^2)
var choice = low + Math.floor(Math.random() * (high - low + 1));
ctx.setClass(choice, 'pivot');
ctx.tag(choice, 'pivot');
ctx.say('Pick a <strong>random pivot</strong>: ' + ctx.arr[choice] +
'. Move it to the end of the range to partition around it.');
await ctx.sleep(1400);
if (choice !== high) {
ctx.swap(choice, high);
ctx.bump('swaps');
}
ctx.clearTags();
ctx.setClass(choice, 'default');
ctx.setClass(high, 'pivot');
ctx.tag(high, 'pivot');
await ctx.sleep(900);
var pivot = ctx.arr[high];
var i = low - 1;
for (var j = low; j < high; j++) {
ctx.bump('comparisons');
ctx.setClass(j, 'comparing');
ctx.say('Is ' + ctx.arr[j] + ' < pivot ' + pivot + ' ?');
await ctx.sleep(850);
if (ctx.arr[j] < pivot) {
i++;
if (i !== j) {
ctx.setClass(i, 'active');
ctx.setClass(j, 'active');
ctx.say('Yes → move it into the smaller-than-pivot region.');
await ctx.sleep(750);
ctx.swap(i, j);
ctx.bump('swaps');
} else {
ctx.say('Yes → it is already in the right region.');
}
ctx.setClass(i, 'left-half');
ctx.setClass(j, j <= i ? 'left-half' : 'default');
await ctx.sleep(600);
} else {
ctx.say('No → leave it on the greater-than-pivot side.');
ctx.setClass(j, 'default');
await ctx.sleep(550);
}
}
ctx.clearTags();
ctx.say('Drop the pivot ' + pivot + ' into the boundary at position ' + (i + 1) + '.');
await ctx.sleep(900);
ctx.swap(i + 1, high);
ctx.bump('swaps');
ctx.setClass(i + 1, 'sorted');
await ctx.sleep(700);
return i + 1;
}
async function sortRange(low, high, depth) {
ctx.stat('depth', depth);
if (low >= high) {
if (low === high) ctx.setClass(low, 'sorted');
return;
}
ctx.say('<strong>Partition</strong> (depth ' + depth + '): positions ' + low + '-' + high + '.');
await ctx.sleep(1000);
var pivotIndex = await partition(low, high);
await sortRange(low, pivotIndex - 1, depth + 1);
await sortRange(pivotIndex + 1, high, depth + 1);
ctx.stat('depth', depth);
}
await sortRange(0, ctx.length - 1, 0);
ctx.stat('depth', 0);
}
});
</script>
</body>
</html>