Guest User

Untitled

a guest
Apr 5th, 2021
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /**
  2. * Write a description of class SelectionSort here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. class SelectionSort
  8. {
  9. void selectionSort(int arr[])
  10. {
  11. int len = arr.length;
  12. for(int i=0; i<len-1; i++)
  13. {
  14.  
  15. //finding the minimum element in the unsorted part of array
  16. int min = i;
  17. for(int j=i+1; j<len; j++)
  18. if(arr[j] < arr[min])
  19. min = j;
  20.  
  21. //swapping the found minimum element with the first
  22. //element of the sorted sub array using temp variable
  23. int temp = arr[min];
  24. arr[min] = arr[i];
  25. arr[i] = temp;
  26. }
  27. }
  28.  
  29. //display the array element
  30. void printArr(int arr[])
  31. {
  32. for(int i=0; i<arr.length; i++)
  33. System.out.print(arr[i] + " ");
  34. System.out.println();
  35. }
  36.  
  37. public static void main(String[] args)
  38. {
  39. SelectionSort obj = new SelectionSort();
  40. int numarr[] = {101,5,18,11,80,67};
  41.  
  42. System.out.print("Before Selection Sort: ");
  43. obj.printArr(numarr);
  44.  
  45. //calling method for selection sorting
  46. obj.selectionSort(numarr);
  47.  
  48. System.out.print("After Selection Sort: ");
  49. obj.printArr(numarr);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment