Skip to main content

thanos_sort

Function thanos_sort 

Source
pub fn thanos_sort<T>(data: &mut Vec<T>) -> Vec<Vec<T>>
where T: Clone,
Expand description

Thanos sorts by randomly halving the array until one survivor remains.

This function mutates the input slice in-place, destroying half of the elements (rounded down) at each iteration until only one remains. It returns a history of all intermediate states.

THIS IS A JOKE. DO NOT USE FOR REAL SORTING.

§Arguments

  • data - A mutable reference to a Vec<T> to be “sorted”

§Returns

  • Vec<Vec<T>> - History of the vector’s state after each snap (including initial state)

§Examples

Basic sorting:

use thanos_sort::thanos_sort;

let mut numbers = vec![4, 2, 7, 1, 9, 3];
let history = thanos_sort(&mut numbers);

// Only one number survives
assert_eq!(numbers.len(), 1);
// We can see the whole history
assert_eq!(history.len(), 4); // 6 -> 3 -> 2 -> 1

Empty vector:

use thanos_sort::thanos_sort;

let mut empty: Vec<i32> = vec![];
let history = thanos_sort(&mut empty);

assert!(empty.is_empty());
assert_eq!(history.len(), 1);
assert!(history[0].is_empty());

Single element (no snaps needed):

use thanos_sort::thanos_sort;

let mut data = vec![42];
let history = thanos_sort(&mut data);

assert_eq!(data, vec![42]);
assert_eq!(history.len(), 1);

Works with any cloneable type:

use thanos_sort::thanos_sort;

let mut strings = vec!["thanos".to_string(), "sort".to_string(), "is".to_string(), "balanced".to_string()];
let history = thanos_sort(&mut strings);

assert_eq!(strings.len(), 1);
assert_eq!(history.len(), 3); // 4 -> 2 -> 1