Selection Sort Algorithm In Java
Tags: sortalgorithms, algorithms, java, selectionsort
Published: 2018-11-27 08:53:33

---
title: Selection Sort Algorithm In Java
published: true
tags: Sort Algorithms,algorithms,java,selection sort
canonical_url: https://www.code2bits.com/selection-sort-algorithm-in-java/
---

Selection sort is a sorting algorithm, specifically an in-place comparison sort and is used for sorting an array of integers. The algorithm divides the input list into two parts, sorted and unsorted. The algorithm looks for the largest element in the unsorted section and swap it with the last position in the unsorted partition.

### Algorithm Classification

The following table contains information about the analysis of the Selection Sort algorithm. It defines the worst, average and best cases in terms of time complexity and also the worst case in space complexity.

| Classification | Value |
| --- | --- |
| Class | Sorting Algorithm |
| Data Structure | Array |
| Time Complexity: Best | Ω(n<sup>2</sup>) |
| Time Complexity: Average | Θ(n<sup>2</sup>) |
| Time Complexity: Worst | O(n<sup>2</sup>) |
| Space Complexity: Worst | O(1) |

Please use the following [link](http://www.bigocheatsheet.com/img/big-o-cheat-sheet-poster.png) for an explanation on Big-O notation and what is good, fair and bad.

### Selection Sort In Java

```java
public final class SelectionSort {
     
    public void sort(int[] collection) {
        if (collection != null) {
            selectionSort(collection);
        } else {
            throw new IllegalArgumentException("Input paramenter for array to sort is null.");
        }
    }
     
    private void selectionSort(int[] collection) {
        int arrayLength = collection.length;
         
        for (int unsortIndex = arrayLength - 1; unsortIndex > 0; unsortIndex--) {
            int largest = 0;
            for (int i = 1; i <= unsortIndex; i++) {
                if (collection[i] > collection[largest]) {
                    largest = i;
                }
            }
            swap(collection, largest, unsortIndex);
        }
    } 
     
    private void swap(int[] collection, int x, int y) {
        int temp = collection[x];
        collection[x] = collection[y];
        collection[y] = temp;
    }   
}
```

#### Sample Code (GitHub)

The details of the SelectionSort class can be viewed [here](https://github.com/Code2Bits/Algorithms-in-Java/blob/master/sort/src/main/java/com/code2bits/algorithm/sort/SelectionSort.java).  
The details of the SelectionSort JUnit Test class can be viewed [here](https://github.com/Code2Bits/Algorithms-in-Java/blob/master/sort/src/test/java/com/code2bits/algorithm/sort/SelectionSortTest.java).

### Conclusion

The Selection Sort algorithm forms part of a larger group of sorting algorithms. Learning through experience is the reason I created this post about the implementation of the Selection Sort algorithm in Java. I have learned a lot about how others have solved the Selection Sort algorithm in other languages including different implementations in Java.

The post [Selection Sort Algorithm In Java](https://www.code2bits.com/selection-sort-algorithm-in-java/) appeared first on [Code2Bits](https://www.code2bits.com).