My question is linked with this one:

Roulette-wheel selection in Genetic algorithm. Population needs to be sorted first?

If we don't sort the population what is the way of organizing roulette wheel selection for it?

Surely, we have to search in linear way now. Have you got any code snippets in C++ or Java for this case?

# C++ – How should roulette wheel selection be organized for non-sorted population in genetic algorithm

c++genetic-algorithmroulette-wheel-selection

## Best Solution

The population does not need to be sorted at all - the key to roulette selection is that the probability of a given individual being selected for reproduction is proportional to its fitness.

Say you have an unsorted population, with fitnesses as follows:

To perform roulette selection, you need only pick a random number in the range 0 to 240 (the sum of the population's fitness). Then, starting at the first element in the list, subtract each individual's fitness until the random number is less than or equal to zero. So, in the above case, if we randomly pick 112, we do the following:

Therefore, we select individual #3 for reproduction. Note how this doesn't require the population to be sorted at all.

So, in pseudocode, it boils down to:

Note that the

`- 1`

in the final line is to counteract the`increment i`

that's done within the last iteration of the loop (because even though we've found the individual we want, it increments regardless).