Core Java

Selection Sort in Java with Algorithm, Example

A quick and practical programming guide to selection sort technique in java with along with the algorithm and examples.

Selection Sort in java

In this tutorial, We will learn about
another sorting technique where auxiliary space is minimized. As of now we have discussed about the following

Implementation of Bubble Sort

Implementation of Optimized Bubble Sort

Implementation of Insertion Sort

Several Java Example programs

In computer science, selection sort is a sorting algorithm, Selection sort works from left to right typically. It finds the smallest element index and its swap with the current indexed element.

It is specifically an in-place comparison sort. It has O(n2) time complexity and worst than Insertion sort and better than Bubble sort.

We will discuss sorting in ascending and descending order for the given input array.

Selection Sort Simulation Example:

Selection sort animation. Red is current min. Yellow is sorted list. Blue is current item

Algorithm:

1
2
3
4
5
6
7
8
Outer Loop from index i 0 to length - 1
 minValueIndex = i;
  Inner loop from index j=i to length - 1
   if (A[minValueIndex] < A[j])
    minValueIndex = j;
   swap (A[minValueIndex], A[i])
  End Inner loop
End Outer loop

Ascending Order Example Program:

package com.adeepdrive.sorting;

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class SelectionSort {
 // Java - W3schools
 public static void main(String[] args) {
  int[] inputArray = { 5, 9, 3, 1, 7 };
  int length = inputArray.length;
  int minValueIndex;
 
  System.out.print("Before sorting input array: ");
  printInputArray(inputArray);
  // Outer Loop
  for (int i = 0; i < length; i++) {
   minValueIndex = i;
   // Inner Loop
   for (int j = i; j < length; j++) {
 
    if (inputArray[minValueIndex] > inputArray[j]) {
     minValueIndex = j;
    }
   }
 
   int temp = inputArray[minValueIndex];
   inputArray[minValueIndex] = inputArray[i];
   inputArray[i] = temp;
   System.out.print("\nIterate " + i + " : ");
   printInputArray(inputArray);
  }
  System.out.print("\nAfter sorting input array: ");
  printInputArray(inputArray);
 }
 
 public static void printInputArray(int[] values) {
  for (int value : values) {
   System.out.print(value + " ");
  }
 }
}

Output:

Before sorting input array: 5 9 3 1 7

Iterate 0 : 1 9 3 5 7

Iterate 1 : 1 3 9 5 7

Iterate 2 : 1 3 5 9 7

Iterate 3 : 1 3 5 7 9

Iterate 4 : 1 3 5 7 9

After sorting input array: 1 3 5 7 9

Descending Order Example Program:

In the above program just change the hi-lighted  line in Yellow background with the following line.

1
if (inputArray[minValueIndex] < inputArray[j]) {

Output:

Before sorting input array: 5 9 3 1 7

Iterate 0 : 9 5 3 1 7

Iterate 1 : 9 7 3 1 5

Iterate 2 : 9 7 5 1 3

Iterate 3 : 9 7 5 3 1

Iterate 4 : 9 7 5 3 1

After sorting input array: 9 7 5 3 1

Other Articles on Core Java

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Selection Sort in java with Algorithm, Example

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button