kids encyclopedia robot

Selection sort facts for kids

Kids Encyclopedia Facts

Selection sort is a simple way to put a list of things, like numbers, into order. Think of it like organizing your toys from smallest to largest. While it's not the fastest method compared to other algorithms like quicksort, it's very easy to understand. Because of this, it's often one of the first sorting methods taught in computer science.

How Selection Sort Works

Selection-Sort-Animation
This animation shows selection sort in action. Watch how the smallest number is found and moved to the front each time.

Imagine you have a list of numbers that are all mixed up. Selection sort divides this list into two parts. One part will hold the numbers that are already sorted. The other part holds the numbers that are still mixed up.

At the very beginning, the "sorted" part is empty. All the numbers are in the "unsorted" part.

The algorithm then looks through all the unsorted numbers. It finds the smallest one. Once it finds the smallest number, it swaps it with the very first number in the unsorted part. This smallest number then moves into the "sorted" part of the list.

The algorithm repeats this process. It finds the next smallest number from the remaining unsorted numbers. It swaps it into the next spot in the sorted part. This continues until all the numbers have been moved from the unsorted part to the sorted part. When this is done, your entire list will be perfectly in order!

Here is an example of selection sort organizing six numbers:

Sorted numbers Unsorted numbers Smallest number found
(43, 91, 73, 89, 12, 20) 12
(12) (91, 73, 89, 43, 20) 20
(12, 20) (73, 89, 43, 91) 43
(12, 20, 43) (89, 73, 91) 73
(12, 20, 43, 73) (89, 91) 89
(12, 20, 43, 73, 89) (91) 91
(12, 20, 43, 73, 89, 91)

Building the Algorithm

To create this sorting method in a computer program, you usually use two loops. A loop is a set of instructions that repeats over and over.

The first, or "outer," loop goes through each position in the list, starting from the left. The second, or "inner," loop helps find the smallest number in the unsorted part of the list. Once the smallest number is found, it is swapped into the correct spot.

Here's how you might write this algorithm in Java:

public static void selectionSort(int[] array) {
    for (int start = 0; start < array.length - 1; start++) {
        int minimumId = start;

        // Find the index of the smallest number
        for (int i = start + 1; i < array.length; i++) {
            if (array[i] < array[minimumId]) {
                minimumId = i;
            }
        }

        // Swap the smallest number with the left-most element
        int temp = array[start];
        array[start] = array[minimumId];
        array[minimumId] = temp;
    }
}

This code tells the computer how to find the smallest number and move it to the correct place, step by step, until the whole list is sorted.

Related pages

See also

Kids robot.svg In Spanish: Ordenamiento por selección para niños

kids search engine
Selection sort Facts for Kids. Kiddle Encyclopedia.