algos 0.6.8

A collection of algorithms in Rust
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#[cfg(feature = "parallel")]
use rayon;
/// Mergesort implementation with parallel processing support.
///
/// This module provides a configurable mergesort implementation that can:
/// - Use insertion sort for small arrays
/// - Process large arrays in parallel using rayon
/// - Handle generic types that implement Ord + Clone
///
/// # Safety
///
/// This implementation uses unsafe code in the following ways:
/// - Uses `split_at_mut` for parallel processing (safe interface to unsafe code)
/// - Uses rayon's parallel execution primitives (safe interface to unsafe code)
///
/// All unsafe operations are properly encapsulated and safe when used with types
/// that implement the required traits (Send + Sync for parallel execution).
use std::fmt::Debug;

use crate::error::{Result, SortError};

/// Builder for configuring and executing merge sort operations.
///
/// # Examples
///
/// ```
/// use algos::cs::sort::mergesort::MergeSortBuilder;
///
/// let mut arr = vec![3, 1, 4, 1, 5, 9];
/// MergeSortBuilder::new().insertion_threshold(16).sort(&mut arr).expect("Sort failed");
/// assert!(arr.windows(2).all(|w| w[0] <= w[1]));
/// ```
///
/// # Performance
///
/// The algorithm has the following complexity characteristics:
/// - Time: O(n log n) in all cases
/// - Space: O(n) auxiliary space
/// - Stable: Yes
///
/// Performance can be tuned through:
/// - `insertion_threshold`: Arrays smaller than this use insertion sort (default: 16)
/// - `max_recursion_depth`: Limit recursion to prevent stack overflow (default: 48)
/// - `parallel`: Enable parallel sorting for large arrays
/// - `parallel_threshold`: Minimum size for parallel processing
#[derive(Debug, Clone)]
pub struct MergeSortBuilder {
    insertion_threshold: usize,
    max_recursion_depth: usize,
    parallel: bool,
    parallel_threshold: usize,
}

impl Default for MergeSortBuilder {
    fn default() -> Self {
        Self {
            insertion_threshold: 16,
            max_recursion_depth: 48,
            parallel: false,
            parallel_threshold: 1024,
        }
    }
}

impl MergeSortBuilder {
    /// Maximum length of slice that can be sorted (2^48 elements).
    /// This limit ensures we don't exceed reasonable memory usage.
    const MAX_LENGTH: usize = 1 << 48;

    /// Creates a new MergeSortBuilder with default settings
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the threshold below which insertion sort is used
    ///
    /// Smaller values favor merge sort's O(n log n) complexity,
    /// larger values favor insertion sort's cache efficiency on small arrays.
    ///
    /// # Examples
    /// ```
    /// use algos::cs::sort::mergesort::MergeSortBuilder;
    ///
    /// let mut arr = vec![5, 2, 8, 1, 9, 3];
    /// MergeSortBuilder::new().insertion_threshold(8).sort(&mut arr).unwrap();
    /// ```
    pub fn insertion_threshold(mut self, threshold: usize) -> Self {
        self.insertion_threshold = threshold;
        self
    }

    /// Sets the maximum recursion depth
    ///
    /// This prevents stack overflow on very large arrays.
    /// The default of 48 supports arrays up to 2^48 elements.
    pub fn max_recursion_depth(mut self, depth: usize) -> Self {
        self.max_recursion_depth = depth;
        self
    }

    /// Enables or disables parallel sorting
    ///
    /// When enabled, arrays larger than the parallel threshold will be sorted
    /// using multiple threads via rayon.
    ///
    /// # Examples
    /// ```
    /// use algos::cs::sort::mergesort::MergeSortBuilder;
    ///
    /// let mut arr = vec![5, 2, 8, 1, 9, 3];
    /// MergeSortBuilder::new().parallel(true).sort(&mut arr).unwrap();
    /// ```
    pub fn parallel(mut self, enabled: bool) -> Self {
        self.parallel = enabled;
        self
    }

    /// Sets the threshold above which parallel sorting is used
    ///
    /// Arrays larger than this threshold will be sorted in parallel
    /// when parallel sorting is enabled.
    pub fn parallel_threshold(mut self, threshold: usize) -> Self {
        self.parallel_threshold = threshold;
        self
    }

    /// Sorts a mutable slice using the configured settings
    ///
    /// # Errors
    ///
    /// Returns `SortError` if:
    /// - Memory allocation fails
    /// - Maximum recursion depth is exceeded
    /// - Input slice is too large (> 2^48 elements)
    /// - Parallel execution fails
    pub fn sort<T>(&self, slice: &mut [T]) -> Result<()>
    where
        T: Ord + Clone + Send + Sync + 'static,
    {
        if slice.len() <= 1 {
            return Ok(());
        }

        if slice.len() > Self::MAX_LENGTH {
            return Err(SortError::input_too_large(slice.len(), Self::MAX_LENGTH));
        }

        // Create auxiliary buffer
        let mut aux = vec![slice[0].clone(); slice.len()];

        if self.parallel && slice.len() >= self.parallel_threshold {
            self.sort_parallel(slice, &mut aux, 0)
        } else {
            self.sort_sequential(slice, &mut aux, 0)
        }
    }

    fn sort_sequential<T>(&self, slice: &mut [T], aux: &mut Vec<T>, depth: usize) -> Result<()>
    where
        T: Ord + Clone + 'static,
    {
        if depth >= self.max_recursion_depth {
            return Err(SortError::recursion_limit_exceeded(
                depth,
                self.max_recursion_depth,
            ));
        }

        if slice.len() <= self.insertion_threshold {
            insertion_sort(slice);
            return Ok(());
        }

        let mid = slice.len() / 2;

        self.sort_sequential(&mut slice[..mid], aux, depth + 1)?;
        self.sort_sequential(&mut slice[mid..], aux, depth + 1)?;

        merge(slice, mid, aux);
        Ok(())
    }

    #[cfg(feature = "parallel")]
    fn sort_parallel<T>(&self, slice: &mut [T], aux: &mut [T], depth: usize) -> Result<()>
    where
        T: Ord + Clone + Send + Sync + 'static,
    {
        if depth >= self.max_recursion_depth {
            return Err(SortError::recursion_limit_exceeded(
                depth,
                self.max_recursion_depth,
            ));
        }

        if slice.len() <= self.insertion_threshold {
            insertion_sort(slice);
            return Ok(());
        }

        let mid = slice.len() / 2;
        let len = slice.len(); // Get length before borrowing
        let (left, right) = slice.split_at_mut(mid);

        // Create auxiliary buffers with the same size as the original
        let mut left_aux = aux[..mid].to_vec();
        let mut right_aux = aux[mid..len].to_vec();

        let (left_result, right_result) = rayon::join(
            || self.sort_sequential(left, &mut left_aux, depth + 1),
            || self.sort_sequential(right, &mut right_aux, depth + 1),
        );

        left_result?;
        right_result?;

        // Merge directly into the original slice
        merge(slice, mid, aux);
        Ok(())
    }

    #[cfg(not(feature = "parallel"))]
    fn sort_parallel<T>(&self, slice: &mut [T], aux: &mut Vec<T>, depth: usize) -> Result<()>
    where
        T: Ord + Clone + Send + Sync + 'static,
    {
        self.sort_sequential(slice, aux, depth)
    }

    /// Validates the size of an array without creating any slices
    ///
    /// # Errors
    ///
    /// Returns `SortError` if:
    /// - Input size is too large (> 2^48 elements)
    pub fn validate_array_size(&self, size: usize) -> Result<()> {
        if size > Self::MAX_LENGTH {
            Err(SortError::input_too_large(size, Self::MAX_LENGTH))
        } else {
            Ok(())
        }
    }
}

/// Sorts a slice using merge sort with default settings
///
/// This is a convenience wrapper around `MergeSortBuilder`.
/// For more control, use `MergeSortBuilder` directly.
///
/// # Errors
///
/// Returns `SortError` if:
/// - Memory allocation fails
/// - Maximum recursion depth is exceeded
/// - Input slice is too large (> 2^48 elements)
/// - Parallel execution fails
pub fn sort<T>(slice: &mut [T]) -> Result<()>
where
    T: Ord + Clone + Send + Sync + 'static,
{
    MergeSortBuilder::new().sort(slice)
}

// Internal helper functions

fn insertion_sort<T: Ord>(slice: &mut [T]) {
    for i in 1..slice.len() {
        let mut j = i;
        while j > 0 && slice[j - 1] > slice[j] {
            slice.swap(j - 1, j);
            j -= 1;
        }
    }
}

fn merge<T>(slice: &mut [T], mid: usize, aux: &mut [T])
where
    T: Ord + Clone,
{
    aux[..slice.len()].clone_from_slice(slice);

    let (left, right) = aux[..slice.len()].split_at(mid);
    let mut i = 0;
    let mut j = 0;
    let mut k = 0;

    while i < left.len() && j < right.len() {
        if left[i] <= right[j] {
            slice[k] = left[i].clone();
            i += 1;
        } else {
            slice[k] = right[j].clone();
            j += 1;
        }
        k += 1;
    }

    if i < left.len() {
        slice[k..].clone_from_slice(&left[i..]);
    }
    if j < right.len() {
        slice[k..].clone_from_slice(&right[j..]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_slice() {
        let mut arr: Vec<i32> = vec![];
        sort(&mut arr).unwrap();
        assert_eq!(arr, Vec::<i32>::new());
    }

    #[test]
    fn test_single_element() {
        let mut arr = vec![1];
        sort(&mut arr).unwrap();
        assert_eq!(arr, vec![1]);
    }

    #[test]
    fn test_sorted_array() {
        let mut arr = vec![1, 2, 3, 4, 5];
        sort(&mut arr).unwrap();
        assert_eq!(arr, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_reverse_sorted() {
        let mut arr = vec![5, 4, 3, 2, 1];
        sort(&mut arr).unwrap();
        assert_eq!(arr, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_random_order() {
        let mut arr = vec![3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
        let mut expected = arr.clone();
        expected.sort();
        sort(&mut arr).unwrap();
        assert_eq!(arr, expected);
    }

    #[test]
    #[cfg(feature = "parallel")]
    fn test_parallel_sorting() {
        // Create a moderately sized array to test parallel sorting
        let size = 10_000; // Reduced from 100_000
        let mut arr: Vec<i32> = (0..size).rev().collect();
        let mut expected = arr.clone();
        expected.sort();

        // Initialize rayon with a custom thread pool for this test
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(4)
            .build()
            .unwrap();

        pool.install(|| {
            MergeSortBuilder::new()
                .parallel(true)
                .parallel_threshold(1000)
                .sort(&mut arr)
                .unwrap();
        });

        assert_eq!(arr, expected);
    }

    #[test]
    #[cfg(feature = "parallel")]
    fn test_parallel_threshold() {
        let size = 10_000;
        let arr: Vec<i32> = (0..size).rev().collect();

        // Set threshold higher than array size - should use sequential sort
        let mut arr1 = arr.clone();
        MergeSortBuilder::new()
            .parallel(true)
            .parallel_threshold((size * 2) as usize)
            .sort(&mut arr1)
            .unwrap();

        // Set threshold lower than array size - should use parallel sort
        let mut arr2 = arr.clone();
        MergeSortBuilder::new()
            .parallel(true)
            .parallel_threshold((size / 2) as usize)
            .sort(&mut arr2)
            .unwrap();

        let mut expected = arr;
        expected.sort();

        assert_eq!(arr1, expected);
        assert_eq!(arr2, expected);
    }

    #[test]
    #[cfg(feature = "parallel")]
    fn test_parallel_stability() {
        #[derive(Debug, Clone, Eq, PartialEq)]
        struct Item {
            key: i32,
            original_index: usize,
        }

        impl PartialOrd for Item {
            fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
                self.key.partial_cmp(&other.key)
            }
        }

        impl Ord for Item {
            fn cmp(&self, other: &Self) -> std::cmp::Ordering {
                self.key.cmp(&other.key)
            }
        }

        // Create a large array of items with duplicate keys
        let size = 10_000;
        let mut items: Vec<_> = (0..size)
            .map(|i| Item {
                key: i as i32 / 10, // Create many duplicates
                original_index: i,
            })
            .collect();

        MergeSortBuilder::new()
            .parallel(true)
            .parallel_threshold(1000)
            .sort(&mut items)
            .unwrap();

        // Verify stability
        for i in 1..items.len() {
            if items[i - 1].key == items[i].key {
                assert!(
                    items[i - 1].original_index < items[i].original_index,
                    "Stability violated at indices {} and {}",
                    i - 1,
                    i
                );
            }
        }
    }

    #[test]
    fn test_recursion_limit() {
        let mut arr: Vec<i32> = (0..10_000).collect(); // Reduced from 1_000_000
        let result = MergeSortBuilder::new()
            .max_recursion_depth(3)
            .sort(&mut arr);

        match result {
            Err(SortError::RecursionLimitExceeded { depth, max_depth }) => {
                assert_eq!(max_depth, 3);
                assert!(depth >= max_depth);
            }
            _ => panic!("Expected RecursionLimitExceeded error"),
        }
    }

    #[test]
    fn test_input_too_large() {
        // Test the error handling without creating any slices
        let size = MergeSortBuilder::MAX_LENGTH + 1;
        let result = MergeSortBuilder::new().validate_array_size(size);

        match result {
            Err(SortError::InputTooLarge { length, max_length }) => {
                assert_eq!(length, size);
                assert_eq!(max_length, MergeSortBuilder::MAX_LENGTH);
            }
            _ => panic!("Expected InputTooLarge error"),
        }
    }
}