Advertisement
Guest User

Untitled

a guest
May 4th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. package com.example.helloworld;
  2.  
  3. public class SortChallenge {
  4.  
  5. public static void getPos(int[][] array, int[] set) {
  6. int i=0;
  7. int j=1;
  8. boolean X;
  9. int count = 0; //initialising i, j and counter and the loop break
  10. for (int a = 0; a < set.length; a++) { //for each number in the set array
  11. X = false;
  12. do {
  13. int pos_i = a / array.length; //sets it's destination i index
  14. int pos_j = a % array.length; //sets it's destination j index
  15. for (i = 0; i < array.length; i++) {
  16. for (j = 0; j < array[i].length; j++) {
  17. if (array[i][j] == set[a]) { //finds the index of the desired number
  18.  
  19. System.out.println("Move Count = " + count);
  20. System.out.println("i = " + i + " j = " + j + " A = " + set[a]);
  21. //System.out.println("pi = " + pos_i + " pj = " + pos_j);
  22. //System.out.println("Current Spot = " + ((j + 1) + (i * 4))); //prints a bunch of shit for error checking
  23. if (j != pos_j) { //if j coordinate is not correct
  24. swapNumbersX(i, j, pos_j, array); //move in the x plane
  25. count += 1;
  26. System.out.println("j.. = " + j + "Pos j = " + pos_j);
  27. }else if(i != pos_i) { //if i coordinate is not correct
  28. swapNumbersY(i, j, pos_i, array); //move in the y plane
  29. count += 1;
  30. }if(i == pos_i && j == pos_j){ //if coordinates are correct
  31. X = true; //break the loop
  32. }
  33. }
  34. }
  35. }
  36.  
  37. //System.out.println("j.. = " + j + "Pos j = " + pos_j);
  38. //System.out.println("i.. = " + i + "Pos i = " + pos_i);
  39.  
  40. } while (X == false); //if i and j are correct values, this will be true
  41.  
  42. }
  43. for (i = 0; i < array.length; i++) {
  44. for (j = 0; j < array[i].length; j++) {
  45. System.out.println((array[i][j]));
  46. }
  47. }
  48. }
  49.  
  50. private static void swapNumbersX(int a, int b, int i, int[][] array) { //swaps numbers left or right depending on size
  51. int temp;
  52. if(i<=b) {
  53. temp = array[a][b];
  54. array[a][b] = array[a][b - 1];
  55. array[a][b - 1] = temp;
  56. }else{
  57. temp = array[a][b];
  58. array[a][b] = array[a][b + 1];
  59. array[a][b + 1] = temp;
  60. }
  61. }
  62.  
  63. private static void swapNumbersY(int a, int b, int i, int[][] array) { //swaps numbers up or down depending on size
  64.  
  65. int temp;
  66. if(i<=a) {
  67. temp = array[a][b];
  68. array[a][b] = array[a - 1][b];
  69. array[a - 1][b] = temp;
  70. }else{
  71. temp = array[a][b];
  72. array[a][b] = array[a + 1][b];
  73. array[a + 1][b] = temp;
  74. }
  75. }
  76.  
  77.  
  78. public static void main(String[] args) { //the main turd, initializes all the shit
  79.  
  80. int[] set = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  81. int[][] input = {{4,6,2,14},{15,8,13,1},{10,5,9,12},{7,11,16,3}};
  82. //int[][] input = {{2,10,5,14},{16,3,9,4},{7,11,15,8},{6,13,12,1}};
  83.  
  84. getPos(input, set);
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement