Skip to content

Selection sort

Java selection sort

Flowchart (ISO 5807)

YesNoStartInput ai = 0, |a| − 1 − 1, 1min = ij = i + 1, |a| − 1, 1a[j] < a[min]min = jt = a[min]a[min] = a[i]a[i] = tEndFigure 1 — Sort.selection

Source code

Java
class Sort {
    static void selection(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[min]) min = j;
            }
            int t = a[min];
            a[min] = a[i];
            a[i] = t;
        }
    }
}

The editor opens with this code — change it and the chart redraws.

Language
Java
Constructs
selection sort
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples