# Selection Sort Java

Hey guys!! Welcome to flower brackets blog. This post explains selection sort java. Let’s see what is selection sort in java.

#### What is selectionsort?

Selection sort is an (sorting)algorithm. It is considered as one of the simplest algorithms.

In selectionsort sorting algorithm, unsorted(lowest) element is shifted to its correct position in an array.

Also Read – Insertion Sort In Java

The selection sorting loop continues until the arrangement of integer elements in array is in the ascending order.

The number of times the sorting takes place will always be one less than the number of integer elements in an array.

#### Example: selection sort java

Let’s see an example on how to implement selection sort in java or selection sort java ascending,

```import java.util.Scanner;

public class SelectionSortInJava
{
public static void toSort(int[] array)
{
int S = array.length;
int a;
int b;
int place;
int temp;

for(a = 0; a < S - 1; a++)
{
place = a;
for(b = a + 1; b < S; b++)
{
if(array[b] < array[place])
{
place = b;
}
}

temp = array[a];
array[a] = array[place];
array[place] = temp;
}
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Best Way To Implement Selection Sort In Java");
int x;
int y;

// accepting user entered integer elements
System.out.println("Please enter number of integer elements");
x = sc.nextInt();

// creating integer array elements
int[] arr = new int[x];

System.out.println("Please enter " + x + " integer elements.");
System.out.println("Integer elements before sorting: ");
for(y = 0;y < x;y++)
arr[y] = sc.nextInt();

// calling toSort method
toSort(arr);

// Displaying sorted array
System.out.println("Integer elements after sorting: ");
for(y = 0;y < x;y++)
System.out.print(arr[y] + " ");
System.out.println();
}
}

```

Output:

Best Way To Implement Selection Sort In Java
Please enter number of integer elements: 5
Integer elements before sorting: 5 4 -3 2 -1
Integer elements after sorting: -3 -1 2 4 5

#### Using method

Let’s see selection sort algorithm or selection sort java ascending using method,

```public class JavaSelectionSort
{
public static int[] selectionSort(int[] arr)
{
// selection sort array java
for(int a = 0; a < arr.length - 1; a++)
{
int index = a;
for(int b = a + 1; b < arr.length; b++)
if(arr[b] < arr[index])
index = b;

int smallNumber = arr[index];
arr[index] = arr[a];
arr[a] = smallNumber;
}
return arr;
}

public static void main(String[] args)
{
int[] arrOne = {10, 23, 32, 14, 27, 45, 36, 21};
int[] arrTwo = selectionSort(arrOne);
for(int x: arrTwo)
{
System.out.print(x);
System.out.print(", ");
}
}
}

```

Output:

10, 14, 21, 23, 27, 32, 36, 45,

##### conclusion

So in this post you learnt what is selection sort java, how does selection sort work, how to implement selection sort in java and selection sort example.

I hope you guys have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.