Objective:

Selection Sort is the Method in which each smallest or large value is transfixed to the initial position in the remaining Unsorted Array thus picking small or big values according to Ascending or Descending Sort respectively. In computer science, a Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
Find the minimum value in the list
Swap it with the value in the first position
Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)
Effectively, the list is divided into two parts: the sublist of items already sorted, which is built up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array.
BlueJ Program Screenshot:
Java Program Source Code:

Selection Sort is the Method in which each smallest or large value is transfixed to the initial position in the remaining Unsorted Array thus picking small or big values according to Ascending or Descending Sort respectively. In computer science, a Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
The algorithm works as follows:
Effectively, the list is divided into two parts: the sublist of items already sorted, which is built up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array.
Here is an example of this sort algorithm sorting five elements:
64 25 12 22 11
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
11 12 22 25 64
(nothing appears changed on this last line because the last 2 numbers were already in order)
Selection sort can also be used on list structures that make add and remove efficient, such as a linked list. In this case it is more common to remove the minimum element from the remainder of the list, and then insert it at the end of the values sorted so far. For example:
64 25 12 22 11
11 64 25 12 22
11 12 64 25 22
11 12 22 64 25
11 12 22 25 64
/* a[0] to a[n-1] is the array to sort */
int iPos;
int iMin;
/* advance the position through the entire array */
/* (could do iPos < n-1 because single element is also min element) */
for (iPos = 0; iPos < n; iPos++) {
/* find the min element in the unsorted a[iPos .. n-1] */
/* assume the min is the first element */
iMin = iPos;
/* test against all other elements */
for (int i = iPos+1; i < n; i++) {
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin]) {
/* found new minimum; remember its index */
iMin = i;
}
}
/* iMin is the index of the minimum element. Swap it with the current position */
if ( iMin != iPos ) {
swap(a, iPos, iMin);
}
}
BlueJ Program Screenshot:
Java Program Source Code:
/**
* The class SelectionSort repetedly selects the smallest key in the remaining unsorted Array.
* @author SHANTANU KHAN
* @mail shantanukhan1995@gmail.com
* @website 0code.blogspot.com
* Program Type : BlueJ Program - Java
*/
import java.io.*;
public class SelectionSort
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int A[],size;
void Input()throws Exception
{
System.out.print("Enter the Size of the Array to be Selection Sorted :");
size=Integer.parseInt(br.readLine());
A=new int[size];
for(int i=0;i<size;i++)
{
int flag=0;
do
{
flag=0;
System.out.print("Enter the Element No."+(i+1)+" : ");
A[i]=Integer.parseInt(br.readLine());
for(int j=0;j<i;j++)
{
if(A[j]==A[i])
{
flag=1;
System.out.println("Duplicate Element Inputted. Try Again");
}
}
}while(flag==1);
}
}
void SelectionAsc()
{
int temp,small,pos;
for(int i=0;i<A.length;i++)
{
small=A[i];
pos=i;
for(int j=i+1;j<A.length;j++)
if(A[j]<small)
{
small=A[j];
pos=j;
}
temp=A[i];
A[i]=A[pos];
A[pos]=temp;
}
}
void SelectionDes()
{
int temp,big,pos;
for(int i=0;i<A.length;i++)
{
big=A[i];
pos=i;
for(int j=i+1;j<A.length;j++)
if(A[j]>big)
{
big=A[j];
pos=j;
}
temp=A[i];
A[i]=A[pos];
A[pos]=temp;
}
}
void Print()
{
System.out.println("Array After Selection Sorting is -->");
for(int i=0;i<A.length;i++)
System.out.print(A[i]+" ");
}
public static void main(String args[])throws Exception
{
int flag=0,choice;
SelectionSort obj=new SelectionSort();
obj.Input();
do
{
System.out.println("\n\n1. Ascending 2. Descending 3. Terminate Program");
System.out.print("Enter Your Choice : ");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
obj.SelectionAsc(); obj.Print();
break;
case 2:
obj.SelectionDes(); obj.Print();
break;
case 3:
System.out.println("Program Successfully Terminated");
flag=1;
break;
default:
System.out.println("INVALID INPUT");
flag=1;
break;
}
}while(flag==0);
}
}





