作爲賦值的一部分,我應該編寫一種方法,根據每個3D數組內部的雙數數量,使用插入排序對4D數組內的3D數組進行排序。插入排序,4維數組Java
我有這個迄今爲止的方法,但似乎如果最小3D陣列在最後一個位置不(four[2]
,其中用於four
是一個四維陣列與3元),只工作。
public static void sort4DArray(double[][][][] list) {
int x;
for (int i = 1; i < list.length; i++) {
double[][][] currentElement = list[i];
//shifts the 3D arrays
for (x = i - 1; x >= 0 && count(list,x) > count(list,i); x--) {
list[x + 1] = list[x];
}
//inserts the 3D array to its new position
list[x + 1] = currentElement;
}
}
請注意,在此方法中,我使用另一種方法「count」來計算給定3D數組內有多少雙打。我將離開下面的代碼以供參考:
public static int count(double[][][][] list, int x) {
int count = 0;
for (int j=0; j < list[x].length; j++) {
for (int k=0; k < list[x][j].length; k++){
count += list[x][j][k].length;
}
}
return count;
}
這裏是兩個採樣輸出,第一個是正確的,第二個是不正確的: 的陣列被印刷在括號格式,希望它是很容易理解。
數組內的數組是隨機長度(不規則),內部的雙數也是隨機生成的。
{
{
{{0.5, 3.6, 8.9, }}
{{26.7, 20.5, 4.7, }}
{{15.3, }}
},
{
{{25.5, }}
},
{
{{15.8, 5.8, 0.2, }{12.7, }}
{{25.8, }}
},
}
After Sort:
{
{
{{25.5, }}
},
{
{{0.2, 5.8, 15.8, }{12.7, }}
{{25.8, }}
},
{
{{0.5, 3.6, 8.9, }}
{{4.7, 20.5, 26.7, }}
{{15.3, }}
},
}
這是第二個輸出。需要注意的是最小的3D陣列是去年在第一印刷:
{
{
{{22.4, }{29.8, }{5.5, }}
{{10.2, 6.4, }}
},
{
{{13.4, }{24.0, }{3.5, 6.0, }}
{{14.1, 8.5, }{5.6, 14.3, }{22.1, }}
},
{
{{20.1, }}
},
}
After Sort:
{
{
{{22.4, }{29.8, }{5.5, }}
{{6.4, 10.2, }}
},
{
{{20.1, }}
},
{
{{13.4, }{24.0, }{3.5, 6.0, }}
{{8.5, 14.1, }{5.6, 14.3, }{22.1, }}
},
}