Skip to main content

Selection Sort in JAVA

What is selection sort?

It is a sorting technique that is based on the partitioning of array into two parts, sorted and unsorted.

The process is:-

1) Find minimum element in unsorted array.
2) Swap the element at the end of sorted array.

when i=0 we don't have any sorted array so element will be replaced from first element later the sorted array end will growas the index.


underline elements are sorted array, the length is increasing with each minimum element added at the end.

minimum element11
 11 25 12 22 64 
minimum element12
 11 12 25 22 64 
minimum element22
 11 12 22 25 64 
minimum element25
 11 12 22 25 64 
minimum element64
 11 12 22 25 64 


package sorting;

public class SelectionSort {

 //function to print array
  public static void print(int[] arr)
   for(int i=0;i<=arr.length-1;i++)
    System.out.print(" "+arr[i]);
   System.out.println(" ");
  //function to swap elements
  public static int[] swap(int[] arr,int i, int j)
   int temp;
   return arr;
  //program to find minimum element in unsorted array( i to end)
  public static int min(int[] arr,int i)
   int min=i;
   System.out.println("minimum element"+arr[min]);
   return min;
  public static void main(String[] args)
   int[] arr={64,25,12,22,11};
   int min;
   for(int i=0;i<=arr.length-1;i++)
    //find minimum element in unsorted array ( i to end)
    //swap minimum element with then end of sorted array(i)
    //print the array



Popular posts from this blog

Adding Node at a given position in Linked List

Adding Node at a given position in Linked List

publicvoidaddNodeAtPosition(Node head,int position,int data){ Node temp=head;//create a node Node node=new Node(data);int c=1;//while counter is not equal to the positionwhile(c!=position){; c++;}//point node to the next of current node;//point next of current to the node to insert;}To Know the basic structure of the linked list click here