# Selection Sort Java – Algorithm 2 Ways | JavaSorting

Implement Selection sort Java We will discuss a couple of methods to Implement Selection Sort In Java Algorithm. The compiler has been added so you can easily execute the given programs, alongside suitable examples and samples outputs added for Selection Sort In Java, For More Sorting Programs in Java Visit here.

• Using Array.

## Selection Sort Algorithm Java – Using Array

Output:

Explanation For Above Algorithm:

1) The entered numbers will store in to the int array a[] using for loop with the structure for( i=0; i < n; i++).

2) Printarray(int a[]) will print the numbers, from the index i=0 to i<length of the array.

3) Sort(int a[]) will sort the numbers in ascending order. The inner loop will find the next least number to the previous number and the outer loop will place the least number in proper position in the array.

Given numbers are 9, 0, 1, 23, 99, 5.

a) The inner loop will compare the first two numbers 9,0 , the least number is 0, then the loop compares 0 with 1, 23, 99, 5. There is no least number available than 0. So outer loop swap the 9,0 numbers. Then the series is 0, 9, 1, 23, 99, 5.

b) Now the inner loop compares the 9,1, the number 1 is the least than 9, then compare 1 with 23, 99, 5. Compare with the next elements, 1 is the least number. Outer loop swap the numbers 9,1. Now the series is 0, 1, 9, 23, 99, 5.

c) Compare 9,23, then the least number is 9, find the least number than 9. In this series 5 is least compare with 9, so the outer loop swap the numbers 9,5. The series is 0, 1, 5, 23, 99, 9.

d) Compare 23,99, the least number is 23, find the least number than 23, 9 is the least number in the remaining series, Outer loop swap the numbers 23,9.Now the series is 0,1,5,9,99,23.

e) Compare 99 with 23, 23 is the least number, swap the numbers 23,99.

After selection sort, the number series is 0, 1, 5, 9, 23, 99.