# Selection Sort in Java

Posted By: Achchuthan Yogarajah - 10:38 PM

### Share

& Comment

The idea of algorithm is quite simple. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Let us see an example of sorting an array to make the idea of selection sort clearer.

Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort.
 Example 1 (http://www.algolist.net/Algorithms/Sorting/Selection_sort)

### Output:

Enter the array size:
3
Enter the array elements:
4
12
1
Before Sorted :Array Elements:
4 12 1
After Sorted :Array Elements:
1 4 12