All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.cilib.util.selection.arrangement.RandomArrangement Maven / Gradle / Ivy

Go to download

A library of composable components enabling simpler Computational Intelligence

There is a newer version: 0.8
Show newest version
/**           __  __
 *    _____ _/ /_/ /_    Computational Intelligence Library (CIlib)
 *   / ___/ / / / __ \   (c) CIRG @ UP
 *  / /__/ / / / /_/ /   http://cilib.net
 *  \___/_/_/_/_.___/
 */
package net.sourceforge.cilib.util.selection.arrangement;

import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.List;
import net.sourceforge.cilib.math.random.generator.Rand;

public class RandomArrangement implements Arrangement {

    @Override
    public Iterable arrange(final Iterable elements) {
        final List list = Lists.newArrayList(elements);
        shuffle(list);
        return list;
    }

    /**
     * Implementation of the Fisher-Yates shuffle algorithm. This algorithm runs in O(n).
     * 

* This method has been added to the implementation due to the fact that Collections.shuffle() * does not perform the same operation efficiently. Collections.shuffle() does not * use the current size of the permutation sublist. * * @param elements The elements to shuffle. */ private void shuffle(List elements) { int n = elements.size(); while (n > 1) { int k = Rand.nextInt(n); // 0 <= k < n n--; Collections.swap(elements, n, k); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy