package array;
import java.util.Arrays;
public class array_Demo06 {
// 冒泡排序法
public static void main(String[] args) {
int[] arrays_1 = {2,1,2,3,11,6,4,9,6,7};
int[] bubbleSort_Arrays = bubbleSort(arrays_1);
System.out.println(Arrays.toString(bubbleSort_Arrays));
}
public static int[] bubbleSort(int[] arrays){
int[] reSort = new int[arrays.length];
for (int i = 1; i < arrays.length; i++) {
if (arrays[i-1] > arrays[i]) {
reSort[0] = arrays[i-1] ; // 将较大的值先寄存到数组 reSort[0] 中
arrays[i-1] = arrays[i];
arrays[i] = reSort[0];
}
}
return arrays;
}
}
==========================================
[1, 2, 2, 3, 6, 4, 9, 6, 7, 11]
Process finished with exit code 0
当一个数组中的大部分元素都是0,或者是为统一数值的数组是,可以使用稀疏数组来保存该数组。
稀疏数组的处理方式是:
记录数组一共有几行几列,有多少不同值把具有不同值的元素和行列及值记录在一个小规模的数组中,从而缩小程序的规模
package array;
public class array_Demo07 {
// 稀疏数组
public static void main(String[] args) {
int[][] array_1 = new int [9][8]; // 定义一个9行8列且有2个不为零的值的二维数组
array_1[1][2] = 3;
array_1[2][3] = 4;
print_2ndArray(array_1);
System.out.println("----------------------");
// 接下来创建稀疏数组
print_2ndArray(sparseArrays(array_1));
System.out.println("-------自定义的稀疏数组-------");
// 输入稀疏数组,反解压出原数组
int[][] sparseArrays_1 = {{8,9,3},{1,1,5},{2,2,6},{3,3,7}}; // 自定义一个稀疏数组
print_2ndArray(sparseArrays_1);
System.out.println("-------解压后的原数组--------");
print_2ndArray(un_sparseArrays(sparseArrays_1));
// 用array_1创建的稀疏数组看看能不能解压出原数组
System.out.println("---------调试数组-----------");
print_2ndArray(un_sparseArrays(sparseArrays(array_1)));
}
public static void print_2ndArray( int[][] arrays){
for (int i = 0 ; i < arrays.length ; i++){
for (int j = 0; j < arrays[i].length; j++) {
// arrays.length 表示行数
// arrays[i].length 表示改行的列数
System.out.print(arrays[i][j]+" ");
if (arrays[i].length-1 == j ){
System.out.print("n");
}
}
}
}
public static int[][] sparseArrays(int[][] arrays){
int sum = 0;
for (int i = 0; i < arrays.length ; i++) {
for (int j = 0; j < arrays[i].length; j++) {
// 检测数组中非0的值,并统计值的个数
if ( arrays[i][j] != 0 ){
System.out.print(arrays[i][j]+" ");
sum++;
}
}
}
System.out.println("该数组非0值有"+sum+"个");
int[][] result = new int[sum+1][3];
// 打印稀疏数组的第一行
result[0][0] = arrays.length;
result[0][1] = arrays[0].length;
result[0][2] = sum;
int n = 1;
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
if ( arrays[i][j] != 0 ){
int m = 0;
result[n][m] = i;
m++;
result[n][m] = j;
m++;
result[n][m] = arrays[i][j];
n++;
}
}
}
return result;
}
public static int[][] un_sparseArrays(int[][] arrays){
int[][] result = new int[arrays[0][0]][arrays[0][1]]; // 定义解压出的result[][]数组的横纵坐标
int n = 0;
int m = 0;
for (int i = 1; i < arrays.length; i++) {
n = arrays[i][0];
m = arrays[i][1];
result[n][m] = arrays[i][2];
}
return result;
}
}
=============================================
0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0
0 0 0 4 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
----------------------
3 4 该数组非0值有2个
9 8 2
1 2 3
2 3 4
-------自定义的稀疏数组-------
8 9 3
1 1 5
2 2 6
3 3 7
-------解压后的原数组--------
0 0 0 0 0 0 0 0 0
0 5 0 0 0 0 0 0 0
0 0 6 0 0 0 0 0 0
0 0 0 7 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
---------调试数组-----------
3 4 该数组非0值有2个
0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0
0 0 0 4 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Process finished with exit code 0



