![](/img/trans.png)
[英]isSorted? Check array if is sorted in ascending or descending order in Java
[英]Ascending order Sorted array is getting printed in descending order?
public void sortcolumn(double[] arraytosort) {
a = new int[5];
for (int i = 1; i < arraytosort.length; i++) {
for (int j = 1; j < arraytosort.length; j++) {
if ((arraytosort[i]) > (arraytosort[j])) {
tempVar = arraytosort[j];
arraytosort[j] = arraytosort[i];
arraytosort[i] = tempVar;
a[i] = j;
}
}
System.out.println( a[i]);
}
}
public void decisionvalue(double[][] arraydc){
arrtemp = new double[arraydc.length];
for(int i =0; i<arraydc[0].length;i++){
System.out.println("The column matrix is");
for(int j =1; j<arraydc.length;j++){
arrtemp[j] = arraydc[j][i];
System.out.println(arrtemp[j]);
}
sortcolumn(arrtemp);
System.out.println("The sorted column matrix is" +i);
for(int k =1; k<arraydc.length; k++)
System.out.println(arrtemp[k]);
}
}
通過上面的代碼,我正在對數組的列進行排序,然后打印它們。 但我不明白為什么升序排序的元素以降序打印。 有人請檢查一下。 謝謝!!
以下是輸出:這是輸入矩陣arraydc
0.0 0.0 0.0
25.0 10.0 24.0
25.0 5.0 23.0
25.0 23.0 6.0
25.0 89.0 8.0
列矩陣為25.0 25.0 25.0 25.0
0 0 0 0
排序的列矩陣為0 25.0 25.0 25.0 25.0
列矩陣為10.0 5.0 23.0 89.0
2 1 2 3
排序的列矩陣是1 89.0 23.0 10.0 5.0
列矩陣為24.0 23.0 6.0 8.0
3 1 2 3
排序的列矩陣是2 24.0 23.0 8.0 6.0
for (int j = i; j < arraytosort.length; j++) {
從i
開始第二個循環
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.