本例是采用反转排序算法实现的。
package Arraypackage;
/**
*
* @author Michael Hou
*
*/
public class Swapping {
public static void main(String[] args) {
// A 2-D array is created
int arr[][]=new int[][] {{1,2,3}, {4,5,6}, {7,8,9}};
//The array is reversed
Swapping sorter = new Swapping();
sorter.sort(arr);
}
/**
*
* @param sort
*
*/
public void sort(int[][]arr) {
for(int i=0; i<arr.length; i++) { // Array iteration
for(int j=0; j<arr[i].length; j++) { // Swapping b/w column and Row
int temp = arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
//All swapping elements output
System.out.println("Swapping array: ");
ShowArray(arr);
}
/**
*
* @param ShowArray
*
*/
public void ShowArray(int[][]arr) {
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print("\t" + arr[j][i]);
}
System.out.println();
}
}
}
本文介绍了一种基于Java的二维数组反转排序算法实现。通过创建一个2-D数组并使用Swapping类的方法进行行和列之间的元素交换,实现了数组的反转排序。此算法通过遍历数组,将每个元素与其对应位置的元素进行交换,从而达到反转排序的效果。
690

被折叠的 条评论
为什么被折叠?



