document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.Scanner;
  2. class Test
  3. {
  4. public static void main(String args[])
  5. {
  6. Scanner obj =new Scanner(System.in);
  7. System.out.println("Enter the number of elements in the array ");
  8. int len=obj.nextInt();
  9. System.out.println("Enter the elements of the array ");
  10. int arr[]=new int[len];
  11. for(int i=0;i<arr.length;i++)//here the elements are being entered into the array
  12. {
  13. arr[i]=obj.nextInt();
  14. }
  15. SelectionSort obj1=new SelectionSort();
  16. obj1.selection(arr);
  17. }
  18. }
  19.  
  20. class SelectionSort
  21. {
  22. void selection(int arr[])
  23. {
  24. int pos,temp;
  25.  
  26. for(int i=0;i<arr.length;i++)//sorting algorithm
  27. {
  28.  
  29. pos=smallest(i,arr);
  30. if(arr[pos]<arr[i])
  31. {
  32. temp=arr[pos];
  33. arr[pos]=arr[i];
  34. arr[i]=temp;
  35. }
  36. }
  37.  
  38. System.out.println("the array in the sorted order is :");
  39. for(int i =0;i<arr.length;i++)
  40. {
  41. System.out.println(arr[i]);
  42. }
  43. }
  44.  
  45. int smallest(int i,int arr[]) // in this function the smallest element is calculated
  46. {
  47. int small=arr[i],pos=i;
  48. for(int k=i+1;k<arr.length;k++)
  49. {
  50. if(arr[k]<small)
  51. {
  52. small=arr[k];
  53. pos=k;
  54. }
  55. }
  56. return pos;
  57. }
  58. }
');