简体   繁体   English

如何以降序对数组进行冒泡排序?

[英]How to BubbleSort an array in descending order?

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;
}

Hello, in the code above I have managed to make it so that it sorts an array in ascending order, however I am fully stuck and stumped on how to edit or change it to make it sort in descending order? 您好,在上面的代码中,我设法使它按升序排序,但是我完全迷住了并陷入如何编辑更改它以使其降序排序的问题上? Any help would be appreciated! 任何帮助,将不胜感激!

Thank you. 谢谢。

All you have to do if you want to reverse sorting (in descending instead of ascemding order) is to reverse the condition: < instead of > : 如果要反转排序( 降序而不是升序 ),您要做的就是反转条件: <而不是>

   ...
   if(numberArray[j] < numberArray[j + 1])
   ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM