//Heap's algorithm from Wikipedia
Heap's algorithm from Wikipedia
Heap ' s algorithm from Wikipedia
fn generate_permutations(input: &[u32]) -> Vec<Vec<u32>> {
    let mut mem = vec![0; input.len()];
    let mut res = vec![];
    res.push(input.to_owned());
    let mut manipulated = input.to_owned();
    let mut i = 0;
    while i < input.len() {
        if mem[i] < i {
            if (i % 2) == 0 {
                manipulated.swap(0, i);
            } else {
                manipulated.swap(mem[i], i);
            }
            res.push(manipulated.clone());
            mem[i] += 1;
            i = 0;
        }  else {
            mem[i] = 0;
            i += 1;
        }
    }
    res
}
fn generate_permutations ( input : & [ u32 ] ) -> Vec < Vec < u32 > > { let mut mem = vec ! [ 0 input len ( ) ] ; let mut res = vec ! [ ] ; res . push ( input . to_owned ( ) ) ; let mut manipulated = input . to_owned ( ) ; let mut i = 0 ; while i < input . len ( ) { if mem [ i ] < i { if ( i % 2 ) == 0 { manipulated . swap ( 0 , i ) ; } else { manipulated . swap ( mem [ i ] , i ) ; } res . push ( manipulated . clone ( ) ) ; mem [ i ] += 1 ; i = 0 ; } else { mem [ i ] = 0 ; i += 1 ; } } res }
Heap's algorithm from Wikipedia
Heap ' s algorithm from Wikipedia
input
&[u32]