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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! Quick Sort implementation
use crate::models::traits::{Sorter, StepResult, Telemetry, Markers};
use std::any::Any;
/// Stack frame for Quick Sort recursion simulation
#[derive(Debug, Clone)]
struct StackFrame {
low: usize,
high: usize,
}
/// State for incremental partitioning
#[derive(Debug, Clone)]
enum PartitionState {
/// No partitioning in progress
NotStarted,
/// Partitioning in progress with current indices and pivot value
InProgress {
current_j: usize,
current_i: usize,
pivot: i32,
low: usize,
high: usize,
},
/// Partitioning completed
Complete,
}
/// Quick Sort algorithm implementation
#[derive(Debug)]
pub struct QuickSort {
data: Vec<i32>,
stack: Vec<StackFrame>,
comparisons: u64,
moves: u64,
complete: bool,
current_pivot: Option<usize>,
memory_usage: usize,
partition_state: PartitionState,
max_progress_seen: f32,
}
impl QuickSort {
/// Create a new QuickSort instance
pub fn new() -> Self {
Self {
data: Vec::new(),
stack: Vec::new(),
comparisons: 0,
moves: 0,
complete: false,
current_pivot: None,
memory_usage: 0,
partition_state: PartitionState::NotStarted,
max_progress_seen: 0.0,
}
}
/// Start a new partition operation
fn start_partition(&mut self, low: usize, high: usize) {
let pivot = self.data[high];
self.partition_state = PartitionState::InProgress {
current_j: low,
current_i: low,
pivot,
low,
high,
};
self.current_pivot = Some(high);
}
/// Continue or complete partition operation with given budget
/// Returns Some(pivot_index) if partition completes, None if more work needed
fn continue_partition(&mut self, budget: &mut usize) -> Option<usize> {
if *budget == 0 {
return None;
}
match &self.partition_state.clone() {
PartitionState::InProgress { current_j, current_i, pivot, low, high } => {
let mut j = *current_j;
let mut i = *current_i;
let pivot_val = *pivot;
let low_bound = *low;
let high_bound = *high;
// Continue partitioning from where we left off
while j < high_bound && *budget > 0 {
*budget -= 1;
self.comparisons += 1;
if self.data[j] <= pivot_val {
if i != j {
self.data.swap(i, j);
self.moves += 1;
}
i += 1;
}
j += 1;
}
if j >= high_bound {
// Partitioning complete - place pivot in final position
if i != high_bound {
self.data.swap(i, high_bound);
self.moves += 1;
}
self.partition_state = PartitionState::Complete;
self.current_pivot = None;
Some(i)
} else {
// More work needed - update state
self.partition_state = PartitionState::InProgress {
current_j: j,
current_i: i,
pivot: pivot_val,
low: low_bound,
high: high_bound,
};
None
}
}
_ => None, // Should not happen when called appropriately
}
}
/// Get partition progress (0.0 to 1.0)
#[allow(dead_code)]
fn get_partition_progress(&self) -> f32 {
match &self.partition_state {
PartitionState::NotStarted => 0.0,
PartitionState::Complete => 1.0,
PartitionState::InProgress { current_j, low, high, .. } => {
if *high <= *low {
1.0
} else {
(*current_j - *low) as f32 / (*high - *low) as f32
}
}
}
}
}
impl Default for QuickSort {
fn default() -> Self {
Self::new()
}
}
impl Sorter for QuickSort {
fn step(&mut self, budget: usize) -> StepResult {
if self.complete || self.data.len() <= 1 {
return StepResult {
comparisons_used: 0,
moves_made: 0,
continued: false,
};
}
let initial_comparisons = self.comparisons;
let initial_moves = self.moves;
let mut remaining_budget = budget;
// Handle ongoing partition if in progress
if matches!(self.partition_state, PartitionState::InProgress { .. })
&& let Some(pivot_pos) = self.continue_partition(&mut remaining_budget) {
// Partition completed - add sub-problems to stack
if let Some(frame) = self.stack.last() {
let current_frame = frame.clone();
self.stack.pop(); // Remove current frame
// Add right subarray if it has more than one element
if pivot_pos + 1 < current_frame.high {
self.stack.push(StackFrame {
low: pivot_pos + 1,
high: current_frame.high,
});
}
// Add left subarray if it has more than one element
if current_frame.low < pivot_pos {
self.stack.push(StackFrame {
low: current_frame.low,
high: pivot_pos - 1,
});
}
}
self.partition_state = PartitionState::NotStarted;
}
// If partition didn't complete, we'll continue it in the next step
// Start new partitions if budget allows and no partition is in progress
while remaining_budget > 0 && !self.stack.is_empty() &&
matches!(self.partition_state, PartitionState::NotStarted) {
let frame = self.stack.last().unwrap().clone();
if frame.low >= frame.high {
self.stack.pop(); // Remove trivial frame
continue;
}
// Start new partition
self.start_partition(frame.low, frame.high);
// Try to make progress on the new partition
if let Some(pivot_pos) = self.continue_partition(&mut remaining_budget) {
// Partition completed immediately
self.stack.pop(); // Remove current frame
// Add right subarray if it has more than one element
if pivot_pos + 1 < frame.high {
self.stack.push(StackFrame {
low: pivot_pos + 1,
high: frame.high,
});
}
// Add left subarray if it has more than one element
if frame.low < pivot_pos {
self.stack.push(StackFrame {
low: frame.low,
high: pivot_pos - 1,
});
}
self.partition_state = PartitionState::NotStarted;
} else {
// Partition started but not completed - will continue next step
break;
}
}
// Check completion
if self.stack.is_empty() && matches!(self.partition_state, PartitionState::NotStarted) {
self.complete = true;
self.current_pivot = None;
}
self.memory_usage = self.stack.len() * std::mem::size_of::<StackFrame>();
// Update progress tracking for monotonicity
self.update_progress();
StepResult {
comparisons_used: (self.comparisons - initial_comparisons) as usize,
moves_made: (self.moves - initial_moves) as usize,
continued: !self.complete,
}
}
fn is_complete(&self) -> bool {
self.complete
}
fn get_telemetry(&self) -> Telemetry {
let mut markers = Markers::default();
if let Some(pivot) = self.current_pivot {
markers.pivot = Some(pivot);
}
// Add cursors for current partition state
match &self.partition_state {
PartitionState::InProgress { current_j, current_i, low, high, .. } => {
markers.cursors = vec![*current_i, *current_j, *low, *high];
}
_ => {
if let Some(frame) = self.stack.last() {
markers.cursors = vec![frame.low, frame.high];
}
}
}
let progress_hint = self.calculate_progress();
let status_text = if self.complete {
"Completed".to_string()
} else {
match &self.partition_state {
PartitionState::InProgress { current_j, low, high, .. } => {
format!("Partitioning range [{}, {}] - progress: {}/{}",
low, high, current_j - low, high - low)
}
_ => {
if let Some(frame) = self.stack.last() {
format!("Partitioning range [{}, {}]", frame.low, frame.high)
} else {
"Processing".to_string()
}
}
}
};
Telemetry {
total_comparisons: self.comparisons,
total_moves: self.moves,
memory_current: self.get_memory_usage(),
memory_peak: self.get_memory_usage(),
highlights: markers.cursors.clone(),
markers,
status_text,
progress_hint,
}
}
fn reset(&mut self, data: Vec<i32>) {
self.data = data;
self.stack.clear();
self.comparisons = 0;
self.moves = 0;
self.complete = self.data.len() <= 1;
self.current_pivot = None;
self.memory_usage = 0;
self.partition_state = PartitionState::NotStarted;
self.max_progress_seen = 0.0;
if !self.complete {
self.stack.push(StackFrame {
low: 0,
high: self.data.len() - 1,
});
self.memory_usage = std::mem::size_of::<StackFrame>();
}
}
fn name(&self) -> &str {
"Quick Sort"
}
fn get_array(&self) -> &[i32] {
&self.data
}
fn get_memory_usage(&self) -> usize {
// Data array + stack memory
self.data.len() * std::mem::size_of::<i32>() +
self.stack.len() * std::mem::size_of::<StackFrame>()
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl QuickSort {
/// Calculate overall progress of the sorting algorithm (monotonic)
fn calculate_progress(&self) -> f32 {
if self.data.len() <= 1 {
return 1.0;
}
if self.complete {
return 1.0;
}
// Base progress on comparisons made vs expected total comparisons
let n = self.data.len() as f32;
let expected_comparisons = n * n.log2(); // O(n log n) average case
let base_progress = (self.comparisons as f32 / expected_comparisons).min(0.95);
// Add fine-grained progress from current partition
let partition_progress = match &self.partition_state {
PartitionState::InProgress { current_j, low, high, .. } => {
if *high > *low {
let current_partition_size = high - low;
let partition_weight = current_partition_size as f32 / n;
let local_progress = (*current_j - *low) as f32 / (*high - *low) as f32;
partition_weight * local_progress * 0.05 // Small contribution for smoothness
} else {
0.0
}
}
_ => 0.0,
};
let current_progress = (base_progress + partition_progress).min(1.0).max(0.0);
// Ensure monotonicity: never decrease progress
self.max_progress_seen.max(current_progress)
}
/// Update progress tracking after a step
fn update_progress(&mut self) {
let current_progress = self.calculate_progress();
self.max_progress_seen = self.max_progress_seen.max(current_progress);
}
}