Guest User

Untitled

a guest
Dec 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String[] args) {
  4. int [] a = new int[]{4,3,1,2};
  5. minimumSwaps(a);
  6. }
  7. static int minimumSwaps(int[] arr) {
  8. int swapsCount = 0;
  9. int arraySize = arr.length;
  10.  
  11. int[] sortedArray = arr.clone();
  12. Arrays.sort(sortedArray);
  13.  
  14. for(int i = 0; i < arraySize; i++) {
  15. if (sortedArray[i] != arr[i])
  16. swapsCount++;
  17.  
  18. for(int j = i+1; j < arraySize; j++) {
  19. if (arr[j] == sortedArray[i]) {
  20. int temp = arr[j];
  21. arr[j] = arr[i];
  22. arr[i] = temp;
  23. break;
  24. }
  25. }
  26. }
  27. return swapsCount;
  28. }
  29. }
Add Comment
Please, Sign In to add comment