二维数组的排序算法,输出的数字是沿对角排序, 注意处理2维数组的下标. 输入可以是hard code的常量,如果是这样,请将输入也打印到控制台。
将二维数组(方阵)以对角线排序方式展示,从右上方到左下方依次由小到大填充:
样例如下:
代码实现:
/*
* Copyright (C), 2002-2018, ********有限公司
* FileName: SortArrayToDisplay.java
* Author: 17092419
* Date: 2018年9月1日 上午10:51:18
* Description: //模块目的、功能描述
* History: //修改记录
* <author> <time> <version> <desc>
* 修改人姓名 修改时间 版本号 描述
*/
package com.roxtiger.common.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/**
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
*
* @author RoxTiger
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class SortArrayToDisplay {
// 数组阶数
private static final int ORDER_ARRAY = 6;
public static int[][] Sort(int[][] input) {
List<Integer> tempList = new ArrayList<Integer>();
for (int[] inputCloum : input) {
for (int key : inputCloum) {
tempList.add(key);
}
}
int length = tempList.size();
// 将数组中的数据从小到大排序
Collections.sort(tempList, new Comparator<Integer>() {
@Override
public int compare(Integer one, Integer two) {
if (one > two) {
return 1;
} else if (one == two) {
return 0;
} else {
return -1;
}
}
});
// 先计算出数组方阵的行数和列数
int order = (int) Math.sqrt(length);
// list中元素索引的最大值
int size = length - 1;
// 计数器,对数组的总操作次数
int fillInCount = 0;
// 根据规律,从最大值填充,每一次要填充的元素个数,比如:5*5中的第一次填充25
// 第二次填充24,23......
int fillIn = 0;
for (int hang = order - 1; hang > -1; hang--) {
// 行下标值
int lineIndex = fillIn;
// 列下标值
int columIndex = order - 1;
// 将数组对角切开,需要填三角元素
// 遍历填充需要方阵阶数次循环,如果达到阶数表示左下半区已填好
if (lineIndex < order) {
for (int j = lineIndex; j >= 0; j--) {
input[columIndex--][lineIndex--] = tempList.get(size--);
fillInCount++;
}
} else {
// 开始填充右半区
lineIndex = (int) (2 * order - 1 - fillIn);
for (int j = lineIndex; j > 0; j--) {
input[(lineIndex--) - 1][columIndex--] = tempList.get(size--);
fillInCount++;
}
}
fillIn++;
if ((hang == 0) && fillInCount < length) {
hang += order - 1;
}
}
return input;
}
/**
* 功能描述: <br>
* 〈功能详细描述〉
*
* @param args
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.println("---请输入6*6方阵的二维数组元素-------");
Scanner scan = new Scanner(System.in);
int[][] sortArray = new int[ORDER_ARRAY][ORDER_ARRAY];
for (int line = 0; line < ORDER_ARRAY; line++) {
for (int colum = 0; colum < ORDER_ARRAY; colum++) {
sortArray[line][colum] = scan.nextInt();
}
}
System.out.println();
System.out.println("---你输入的二维数组如下-------");
for (int[] tempa : sortArray) {
for (int key : tempa) {
if (key < 10) {
System.out.print(" " + key + " ");
} else if (key > 9 && key < 100) {
System.out.print(" " + key + " ");
} else {
System.out.print(key + " ");
}
}
System.out.println();
}
System.out.println("---格式化后的的二维数组如下-------");
int[][] kk = Sort(sortArray);
for (int[] aa : kk) {
for (int key : aa) {
if (key < 10) {
System.out.print("00" + key + " ");
} else if (key > 9 && key < 100) {
System.out.print("0" + key + " ");
} else {
System.out.print(key + " ");
}
}
System.out.println();
}
}
}
运行结果如下:
---请输入6*6方阵的二维数组元素-------
123 456 789 987 654 321 145 156 178 189 154 156 157 18 159 2 4 3 7 5 9 6 74 152 37 45 64 334 774 52 46 343 422
78 942 445
---你输入的二维数组如下-------
123 456 789 987 654 321
145 156 178 189 154 156
157 18 159 2 4 3
7 5 9 6 74 152
37 45 64 334 774 52
46 343 422 78 9424 445
---格式化后的的二维数组如下-------
123 046 009 005 003 002
157 145 052 018 006 004
334 159 152 064 037 007
456 343 178 154 074 045
789 654 422 189 156 078
942 987 774 445 321 156