document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.*;
  2. public class SelSortExample {
  3. //Method that implements Selectionsort
  4. public static void selsort(int[] arr)
  5. {
  6. int n=arr.length; //length of the array
  7. for(int i=0;i<n-1;i++)
  8. {
  9. int MIN=i; //set the first position as minimum
  10. System.out.println("Sorting based on Number "+(i+1));
  11. //Find the smallest element by comparing with the element in MIN position
  12. for(int j=i+1;j<n;j++)
  13. {
  14. System.out.println("Comparing "+ arr[MIN] + " and " + arr[j]);
  15. if(arr[j]<arr[MIN])
  16. {
  17. System.out.println(arr[MIN] + " is greater than " + arr[j] );
  18. MIN=j;
  19. }
  20. }
  21. //Swap the smallest element with element in MIN position
  22. int temp=arr[i];
  23. arr[i]=arr[MIN];
  24. arr[MIN]=temp;
  25. }
  26. }
  27. public static void main(String[] args) {
  28. int[] arr= {15,21,6,3,19,20}; // input array
  29. System.out.println("Elements in the array before Sorting: "+ Arrays.toString(arr));
  30. selsort(arr);//calling the selection sort method
  31. System.out.println("Elements in the array after Sorting: "+Arrays.toString(arr));
  32. }
  33. }
');