Algorithm, Algorithms In Java, Software Development

Selection Sort Algorithm Implementation in Java

This post shows how to implement the Selection Sort algorithm in Java. The selection sort algorithm is one of the most common algorithms we use and is more efficient than bubble sort.

Requirements

We use the following items for this post.

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)

Selection Sort Algorithm Description

Selection sort is an algorithm that processes a list of values by dividing it into two parts as it works on data – the sorted and unsorted portions. Consider the following table.
java-selection-sort
First, we start with an integer set in decreasing order from 9 to 1. Beginning with the first number on the left, 9, exchange it with the smallest value from the right. Then, we move to the second integer, 8, and exchange it with the smallest value from its right side. Repeat the operation until we reach the last element. All in all, there are eight operations but only four exchanges for this example.

Selection Sort Implementation In Java

The following code snippets implement the Selection sort in Java. Moreover, it uses a predefined list of integers in no particular order. The codes also have the sort method that accepts two arguments. First, we have an ordered list of integers. Second, we supply a boolean sort direction – ascending (true) or descending (false).

Consider the following sort method that implements the sorting algorithm.

Get Codes from GitHub

The complete Java codes for our Selection sort algorithm are available on GitHub.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like