Hello Friends,

In this post, we will talk and learn about **How to Write a Java program for Selection Sort in Java**

Selection sort is considered as faster than bubble sort as the number of swaps is lesser but the comparison is still proportional to N2

**How selection sort works**

In selection sort usually, we aim is to bring the lowest element to the 0th index in the first pass or iteration. In the next pass or iteration, the second-lowest to the 1st index and so on.

We start from the 0th index and consider it is as the lowest. Then compare it with the other elements, if any element is smaller than the first element then that element becomes the lowest for further comparisons in that pass or iteration. That way we have the lowest element at the end of the pass or iteration it is then swapped with the element at the 0th index.

Similarly, in the next pass or iteration, We start with the element at the 1st index and consider it is the second-lowest element and then compare it with all the elements on its right, in between if any element smaller than this element is found then further comparisons are done using this smallest element.

For example, if we have an array [50, 20, 60, 10] then in the first iteration-

Initially, We will start with 50 (element at 0th index) and compare it with elements on its right. Since 20 is smaller than 50 so now 20 is considered the lowest.

Then 20 is compared with 60. Still, 20 is the lowest.

Then 20 is compared with 10 since 10 is smaller so it is now the lowest element and the end of the array is also reached. Swap 10 with 50 so after the first iteration array is [10, 20, 60, 50].

In the next pass or iteration, we will start with 20 (element at the 1st index) and compare it with elements on its right using the same logic for sorting.

__The logic used in the Selection Sort Java Program:__

There are two nested for loops, the outer loop starts from the leftmost element and goes till the one less than the number of elements in the array.

In the inner loop, we start at the index one more than the current index of the outer loop and loop until the end of the array.

Within the inner loop, elements are compared with the element currently pointed by the outer loop to get the lowest element within that iteration. Once the element is found it is swapped with the element at the current index of the outer loop.

**Below is the complete source code.**

1 2 3 4 5 6 7 8 9 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.kkjavatutorials.client; import java.util.Arrays; import java.util.Scanner; /** * Java program for Selection Sort * @author KK JavaTutorials * */ public class SelectionSortTest { public static void main(String[] args) { Scanner scanner = null; try { System.out.println("Enter input Array Size:"); scanner = new Scanner(System.in); int inputArraySize = scanner.nextInt(); int inputArray[] = new int[inputArraySize]; System.out.println("Enter " +inputArraySize+" Array Elements:"); for (int i = 0; i < inputArray.length; i++) { inputArray[i] = scanner.nextInt(); } System.out.println("Original Array::"); System.out.println(Arrays.toString(inputArray)); int sortedArray[] = selectionSort(inputArray); System.out.println("Sorated Array::"); System.out.println(Arrays.toString(sortedArray)); } catch (Exception e) { e.printStackTrace(); }finally { if(scanner != null) scanner.close(); } } private static int[] selectionSort(int[] inputArray) { for (int i = 0; i < inputArray.length-1; i++) { int smallestIndex = i; for (int j = i+1; j < inputArray.length; j++) { if(inputArray[j] < inputArray[smallestIndex]) { smallestIndex = j; } } int smallestElement = inputArray[smallestIndex]; inputArray[smallestIndex] = inputArray[i]; inputArray[i] = smallestElement; } return inputArray; } } |

**Sample input/output of the above program**

Enter 6 Array Elements:

123

23

45

67

10

29

Original Array::

[123, 23, 45, 67, 10, 29]

Sorted Array::

[10, 23, 29, 45, 67, 123]

**Time and space complexity of Selection sort:**

In selection sort, We use two nested loops that go through the elements that make the complexity of N*N i.e. time complexity of Selection sort is O(N2) and the total number of comparisons are N*(N-1)/2.

In selection sort, the number of swaps is usually less in comparison to the bubble sort that makes it faster than the bubble sort.

Selection sort is also an example of **“in place”** sorting algorithm so apart from the input array there is no auxiliary space requirement so the space complexity of Selection sort is O(1)

**You May Also Like:**

Count total number of times each character appears in the string in java

Check if two strings are anagrams or not in java

How to convert string to int without using library functions in java

Check Whether a Given String/Number is a Palindrome in java

How to find first non-repeated character in a given String in Java

How to find first non-repeatable character from a String using Java 8

That’s all about **How to Write a Java program for Selection sort using the Iterative Approach?**

If you have any feedback or suggestion please feel free to drop in below comment box.