# Selection Sort In Java With Example Code

In this Selection Sort In Java tutorial, we will discuss all the things you need to know about this sort algorithm, as well as how it is implemented. This tutorial could be one of your fundamental learning guides on your journey as a Java developer.

## What Is Selection Sort In Java?

The Selection Sort In Java Program algorithm selects the smallest element in the array and swaps it with the array’s first element. The array’s second smallest element is then swapped with the second element, and vice versa.

In each iteration of the selection sort algorithm, the smallest element from an unsorted list is chosen and placed at the beginning of the unsorted list.

## Implementation of a Selection Sort

Let’s talk about how this selection sort works.

• Make the first element the most minimal.
• When comparing the first and second elements, keep in mind that the first is the most important. Assign the second element as the minimum if it is smaller than the first.

Consider the third factor in comparison to the minimum. Assign a minimum to the third element if it is smaller; otherwise, do nothing. The procedure continues until the last element.

How To Sort Alphabetically - MS Exc...
How To Sort Alphabetically - MS Excel
• After each iteration, minimum is moved to the front of the unsorted list.
• Indexing begins with the first unsorted element for each iteration. Repeat steps 1–3 until all of the elements are in their proper places.

In the fourth iteration, you can see that the iteration is finished. From the left side of all the elements, the numbers go from lowest to highest (3, 11, 13, 16, 21). This means that the Selection Sort is complete and in perfect order.

## Algorithm Of Selection Sort

```selection_sort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < current_minimum
set element as new minimum
swap minimum with the first unsorted position
end selection_sort```

## Selection Sort Sample Code

You can run this code in your code editor if you already downloaded it to your laptop or PC, but if you don’t, you can download it. Here’s the link. NetBeans IDE, but if you wish to run this code online, we also have an online compiler in Java. Here’s the link. Java Online Compiler

We also put the Java online compiler below. Just scroll down so that you can easily test or run the Selection Sort source code in order to learn more about this sort algorithm.

## Sample Java Code Using Scanner.

Scanner is a class in the Java Utilities package that is used to get the input of primitive types like int, double, etc., and strings. It is the easiest way to read input into a Java program

```package selectionsort_in_java;

import java.util.Scanner;
public class SelectionSort_in_Java {

public static void Sort(int a[]) {
int n = a.length, i, j, p, temp;
for (i = 0; i < n - 1; i++) {
p = i;
for (j = i + 1; j < n; j++) {
if (a[p] > a[j]) {
p = j;
}

}
temp = a[p];
a[p] = a[i];
a[i] = temp;

}
}

public static void printarray(int a[]) {
for (int i = 0; i < a.length; i++) {

System.out.print(a[i] + " ");
}

}

public static void main(String[] args) {
int n, res, i;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter " + n + " elements ");
for (i = 0; i < n; i++) {
a[i] = s.nextInt();
}

System.out.println("elements in array ");
printarray(a);
Sort(a);
System.out.println("\nelements after sorting");
printarray(a);
}

}
```

## Selection Sort Example Output Using Scanner

```Enter number of elements in the array:Enter 5 elements
elements in array
21 13 11 16 3
elements after sorting
3 11 13 16 21 ```

## Sample Java Code Using Array.

An Array is a container object that holds a fixed number of values of the same type. The length of an array is set when the array is made. After it is made, its length is fixed.

```// Selection sort in Java by Glenn Magada Azuelo

import java.util.Arrays;

public class SelectionSort {
void selectionSort(int array[]) {
int size = array.length;

for (int step = 0; step < size - 1; step++) {
int min_idx = step;

for (int i = step + 1; i < size; i++) {

// To sort in descending order, change > to < in this line.
// Select the minimum element in each loop.
if (array[i] < array[min_idx]) {
min_idx = i;
}
}

// put min at the correct position
int temp = array[step];
array[step] = array[min_idx];
array[min_idx] = temp;
}
}

// driver code
public static void main(String args[]) {
int[] data = { 21, 13, 11, 16, 3 };
SelectionSort ss = new SelectionSort();
ss.selectionSort(data);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}```

## Selection Sort Example Output Using Array

```Sorted Array in Ascending Order:
[3, 11, 13, 16, 21]```

## Test Your Selection Sort Code

Anyway, if you want to level up your programming knowledge, especially Java, try this new article I’ve made for you Best Java Projects With Source Code For Beginners Free Download 2021.

## Conclusion

So far, we’ve already discussed the Selection Sort algorithm for sorting in Java. We also looked at the algorithm and a detailed example of how to use the Selection Sort technique to sort an array and using scanner. Then we put the Java program to work on the Selection Sort. I hope this Selection Sort tutorial helps you a lot in learning Java development.

## Related Sorting Algorithm in Java

Quick Sort

Bubble Sort In Java