Java Selection sort example

Selection sort is an in-place comparison sort. It loops and find the first smallest value, swaps it with the first element; loop and find the second smallest value again, swaps it with the second element, repeats third, fourth, fifth smallest values and swaps it, until everything is in correct order.

P.S Selection sort is inefficient on large lists

1. Explanation

#unsorted data -> [10, 8, 99, 7, 1, 5, 88, 9]

#1 -> [10, 8, 99, 7, 1, 5, 88, 9] -> [1, 8, 99, 7, 10, 5, 88, 9]
#2 -> [1, 8, 99, 7, 10, 5, 88, 9] -> [1, 5, 99, 7, 10, 8, 88, 9]
#3 -> [1, 5, 99, 7, 10, 8, 88, 9] -> [1, 5, 7, 99, 10, 8, 88, 9]
#4 -> [1, 5, 7, 99, 10, 8, 88, 9] -> [1, 5, 7, 8, 10, 99, 88, 9]
#5 -> [1, 5, 7, 8, 10, 99, 88, 9] -> [1, 5, 7, 8, 9, 99, 88, 10]
#6 -> [1, 5, 7, 8, 9, 99, 88, 10] -> [1, 5, 7, 8, 9, 10, 88, 99]
#7 -> [1, 5, 7, 8, 9, 10, 88, 99]  -> [1, 5, 7, 8, 9, 10, 88, 99]

#result : [1, 5, 7, 8, 9, 10, 88, 99]

Here’s the Java Selection sort implementation.


	public static void sort(int[] input) {

        int inputLength = input.length;

        for (int i = 0; i < inputLength - 1; i++) {

            int min = i;

            // find the first, second, third, fourth... smallest value
            for (int j = i + 1; j < inputLength; j++) {
                if (input[j] < input[min]) {
                    min = j;
                }
            }

            // swaps the smallest value with the position 'i'
            int temp = input[i];
            input[i] = input[min];
            input[min] = temp;

            //next pls
        }

    }

2. Java Selection sort example

A full example to demonstrate the use of Selection sort algorithm to sort a simple data set.

SelectionSortExample.java

package com.mkyong;

import java.util.Arrays;

public class SelectionSortExample {

    public static void main(String[] args) {

        int[] array = {10, 8, 99, 7, 1, 5, 88, 9};

        selection_sort(array);

        System.out.println(Arrays.toString(array));

    }

    private static void selection_sort(int[] input) {

        int inputLength = input.length;

        for (int i = 0; i < inputLength - 1; i++) {

            int min = i;

            // find the first, second, third, fourth... smallest value
            for (int j = i + 1; j < inputLength; j++) {
                if (input[j] < input[min]) {
                    min = j;
                }
            }

            // swaps the smallest value with the position 'i'
            int temp = input[i];
            input[i] = input[min];
            input[min] = temp;

            //next pls
        }

    }

}

Output


[1, 5, 7, 8, 9, 10, 88, 99]

References

  1. Different kind of Sorting
  2. Wikipedia Sorting algorithm - Selection sort

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Kiran Malhotra Recent comment authors
newest oldest most voted
Kiran Malhotra
Guest
Kiran Malhotra

Thanks for sharing the descriptive information on Java tutorial. It’s really helpful to me since I’m taking Java training. Keep doing the good work and if you are interested to know more on Java, do check this Java tutorial.:-https://www.youtube.com/watch?v=CylZMxAe2Js