Selection Sort Algorithm in Java

In this article, we will discuss working and implementation of Selection sort algorithm.


Selection Sort Overview

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving the unsorted array boundary by one element to the right.
This algorithm is not suitable for large datasets as its average and worst case complexities are of Ο(n2), where n is the number of items.

How Selection Sort Works?

Consider the following depicted integer array as an example.
Selection Sort Algorithm in Java
For the first position in the sorted list, the whole list is scanned sequentially. The first position where 20 is stored presently, we search the whole list and find that 15 is the lowest value.
Selection Sort Algorithm in Java
So we replace 20 with 15. After one iteration 15, which happens to be the minimum value in the list, appears in the first position of the sorted list.
Selection Sort Algorithm in Java
For the second position, where 23 is residing, we start scanning the rest of the list in a linear manner.
Selection Sort Algorithm in Java
We find that 20 is the second lowest value in the list and it should appear in the second place. We swap these values.
Selection Sort Algorithm in Java
After two iterations, the two least values are positioned at the beginning in a sorted manner.
Selection Sort Algorithm in Java
Now, scan the rest of the list in a linear manner and check third smallest value in the list 21 and it stayed in the same position.
Selection Sort Algorithm in Java
Let's scan the rest of the list in a linear manner and find fourth smallest value in the list that 23 and replace 25 with 23.
Selection Sort Algorithm in Java
This is an end of an array, after sorting array the final output looks as below:Selection Sort Algorithm in Java

Advantages

  • Easy to implement
  • In-place sort (requires no additional storage space)

Disadvantages

  • Doesn’t scale well: O(n2)
This algorithm is called selection sort since it repeatedly selects the smallest element.

Let's implement Selection Sort algorithm using Java.

Selection Sort Algorithm Implementation in Java 

import java.util.Arrays;

/**
 * Class demonstrate the Selection sort algorithm
 * @author Ramesh Fadatare 
 *
 */
public class SelectionSort {

    void selectionSort(int arr[]) {
        int size = arr.length;

        // One by one move boundary of unsorted subarray
        for (int i = 0; i < size - 1; i++) {
            // Find the minimum element in unsorted array
            int minIndex = i;
            for (int j = i + 1; j < size; j++)
                if (arr[j] < arr[minIndex])
                    minIndex = j;

            // Swap the found minimum element with the first
            // element
             int temp = arr[minIndex];
             arr[minIndex] = arr[i];
             arr[i] = temp;
        }
 }

    // Driver code to test above
    public static void main(String args[]) {
         SelectionSort selectionSortb = new SelectionSort();
         int arr[] = { 20,10,5,6,2,3,4};
  
         System.out.println("Before Sorting an array : " + Arrays.toString(arr));
         selectionSortb.selectionSort(arr);
         System.out.println("After Sorting an array : " + Arrays.toString(arr));
    }
}
Output:
Before Sorting an array : [20, 10, 5, 6, 2, 3, 4]
After Sorting an array : [2, 3, 4, 5, 6, 10, 20]

Performance

  1. Worst case complexity: O(n2)
  2. Best case complexity: O(n2)
  3. Average case complexity: O(n2)
  4. Worst case space complexity: O(1) auxiliary

Comments