構造サイクロトロン行列
タイトル:N*N配列を内旋または外旋で並べ替える
- import java.util.Arrays;
- public class Sort {
- public static void main(String[] args){
- int[][] array =
- {{12,2,11, 8,17},
- {1,13, 3, 9,18},
- {4,10,16, 7,19},
- {5,15,14, 6,20},
- {25,23,24,21,22}};
- boolean isClockwise = false;//
- ringSort(array, isClockwise);
- print(array);
- }
-
- private static int[][] orientsClockwise = {{0,1},{1,0},{0,-1},{-1,0}};
- //private static int[][] orientsUnClockwise = {{1,0},{0,1},{-1,0},{0,-1}};
-
- private static int[][] ringSort(int[][] array, boolean isClockwise) {
- boolean out2in = true;//
- int[] one = pase2array(array);
- empty(array);
- if(out2in){
- Arrays.sort(one);
- }else{
- Arrays.sort(one);
- for (int i = 0; i < one.length/2; i++) {
- one[i] = one[i]^ one[one.length-1-i];
- one[one.length-1-i] = one[i]^one[one.length-1-i];
- one[i] = one[i]^one[one.length-1-i];
- }
- }
-
- print(one);
- //
- int curOrient = 0;
- int i = 0, j = 0;
- for (int k = 0; k < one.length; k++) {
- array[i][j] = one[k];
- int[] newdir = nextOrient(curOrient, array, i, j);
- i = newdir[0];
- j = newdir[1];
- curOrient = newdir[2];
- }
- return array;
-
- }
-
- private static int[] nextOrient(int curOrient, int[][] array, int i, int j) {
- int nextOrient = curOrient;
- int row = array.length;
- int column = array[0].length;
- int ii = i + orientsClockwise[curOrient][0];
- int jj = j + orientsClockwise[curOrient][1];
- if(ii==row||ii==-1||jj==-1||jj==column||array[ii][jj]!=0){
- nextOrient =++nextOrient%4;
- }
- i += orientsClockwise[nextOrient][0];
- j += orientsClockwise[nextOrient][1];
-
- System.out.println(i+" "+j);
-
- int[] newdir = {i,j,nextOrient};
- return newdir;
- }
- }