Pin It

How to sort array elements using Selection Sort algorithm in Java ?

Program to demonstrate how to sort array elements using Selection Sort algorithm in Java

public class SelectionSort {
   
   public static void main(String[] args) {
  
 int[] a = {10,2,3,4,7,8,9,0};
  
 System.out.println("Before Selection Sort: ");
 for(int k = 0 ; k < a.length; k++)
            System.out.print(a[k] + " ");
  
 int min =0 , out =0 , in = 0;
  
 for(out = 0; out < a.length; out++){
            min = out;
   
            for(in = out+1; in < a.length; in++ ){
  if(a[min] < a[in]){
                   min = in;
  }
     }
     int temp = 0;
     temp = a[min];
     a[min] = a[out];
     a[out] = temp;
 }
  
 System.out.println("\nAfter Selection Sort: ");
        for(int k = a.length - 1; k >= 0; k--)
     System.out.print(a[k] + " ");
 }

}




Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot