2013-05-10 5 views
-2

Je pourrais trouver la réponse dans google, ainsi peut-être vous pouvez me suggérer quelque chose.Tri de matrice multidimensionnel (serpent)

La tâche est:

  1. Creat tableau multidimension avec des chiffres aléatoires - Fait;
  2. Tri de la matrice multidimensionnelle - Terminé;
  3. Trier un tableau multidimensionnel (serpent) - Non terminé;

Ainsi, pour eample:

Nous avons: tableau Classé:

  • [5] [2] [4]
  • [1] [3] [6]
  • [9] [7] [8]

Comment peut-on classer dans le type de serpent:

  • [1] [2] [3]
  • [4] [5] [6]
  • [7] [8] [9]

Il y a un code à deux tâches DONE:

public class Snake { 

public static void main(String[] args) { 

int line = 3; 
int column = 3; 

new Snake().sort(line, column); 

} 

Random rnd; 

public void sort(int line, int column) { 
rnd = new Random(); 
int temp = 0; 
int[][] arr = new int[line][column]; 
System.out.println("Unsorted array:"); 
for (int i = 0; i < line; i++) { 
    for (int j = 0; j < column; j++) { 
    arr[i][j] = rnd.nextInt(50) + 1; 
    System.out.print("[" + arr[i][j] + "]"); 

    } 
    System.out.println(); 
} 

for (int i = 0; i < line; i++) { 
    for (int j = 0; j < column; j++) { 
    for (int k = 0; k < column - 1; k++) { 
     if (arr[j][k] > arr[j][k + 1]) { 
     temp = arr[j][k]; 
     arr[j][k] = arr[j][k + 1]; 
     arr[j][k + 1] = temp; 
     } 
    } 
    } 
} 

System.out.println("Sorted array:"); 
for (int i = 0; i < line; i++) { 
    for (int j = 0; j < column; j++) { 
    System.out.print("[" + arr[i][j] + "]"); 
    } 
    System.out.println(); 

} 

} 

}

+0

Jetez un oeil http://stackoverflow.com/questions/2464469/formula- needed-sort-array-to-array-snaked –

Répondre