【发布时间】:2019-01-01 03:40:51
【问题描述】:
private static double[] BubbleSortAscending(double[] numberArray)
{
int arrayLength = numberArray.Length;
for(int i = 0; i < arrayLength - 1; i++)
{
for(int j = 0; j < arrayLength - 1 - i; j++)
{
if(numberArray[j] > numberArray[j + 1])
{
double num = numberArray[j];
numberArray[j] = numberArray[j + 1];
numberArray[j + 1] = num;
}
}
}
return numberArray;
}
您好,在上面的代码中,我设法使它按 升序 顺序对数组进行排序,但是我完全被困在如何编辑上或更改它以使其按降序顺序排序?任何帮助将不胜感激!
谢谢。
【问题讨论】:
-
反向条件:
if(numberArray[j] < numberArray[j + 1]) -
不是最有效的解决方案:只需在最后反转您的数组
-
这能回答你的问题吗? Bubble sort strings in descending order
标签: c# sorting bubble-sort descendant