Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Bubblesort按相反顺序打印_C# - Fatal编程技术网

C# Bubblesort按相反顺序打印

C# Bubblesort按相反顺序打印,c#,C#,但是,我已经编写了一个bubblesort,当我打印出数组时,它被排序,但它以最高的数字开始,以最低的数字结束 public int[] BubbleSort(int[] unsortedArray) { for(int i = 0; i < unsortedArray.Length; i++) for(int j = 0; j < unsortedArray.Length; j++) if(unsortedArray[i] <

但是,我已经编写了一个bubblesort,当我打印出数组时,它被排序,但它以最高的数字开始,以最低的数字结束

public int[] BubbleSort(int[] unsortedArray)
{
    for(int i = 0; i < unsortedArray.Length; i++)
        for(int j = 0; j < unsortedArray.Length; j++)
            if(unsortedArray[i] < unsortedArray[j])
            {
                int temp = unsortedArray[i];
                unsortedArray[i] = unsortedArray[j];
                unsortedArray[j] = temp;
            }
    return unsortedArray;
}
public int[]BubbleSort(int[]unsortedArray)
{
for(int i=0;i
有人能解释一下为什么清单颠倒了吗

编辑:对不起,粘贴了错误的代码

当这行读到 if(unsortedArray[i]这是有条件的

if(unsortedArray[i] < unsortedArray[j])
if(unsortedArray[i]
应该是

if(unsortedArray[j] < unsortedArray[i])
if(unsortedArray[j]
编辑:回答您的编辑

您希望
unsortedArray[i]
中的元素在运行内部循环后具有最低值。这意味着您只有在遇到
unsortedArray[j]
时才将其关闭,该值小于
unsortedArray[i]
中的当前值

如果
unsortedArray[i]
已经是较低的值,则将其保留在原来的位置。

就是这个条件

if(unsortedArray[i] < unsortedArray[j])
if(unsortedArray[i]
应该是

if(unsortedArray[j] < unsortedArray[i])
if(unsortedArray[j]
编辑:回答您的编辑

您希望
unsortedArray[i]
中的元素在运行内部循环后具有最低值。这意味着您只有在遇到
unsortedArray[j]
时才将其关闭,该值小于
unsortedArray[i]
中的当前值

如果
unsortedArray[i]
已经是较低的值,则将其保留在原来的位置。

您的比较结果

 if(unsortedArray[i] < unsortedArray[j])
你的比较

 if(unsortedArray[i] < unsortedArray[j])
最好是:

public int[] BubbleSort(int[] unsortedArray)
{
    return unsortedArray.OrderBy(x=>x).ToArray();
}
原始版本的一些问题,
i
j
迭代太多了,它仍然可以工作,但它进行了不必要的迭代,不会影响结果,而且您的条件
unsortedArray[i]
是向后的

public int[] BubbleSort(int[] unsortedArray)
{
    for(int i = 0; i < unsortedArray.Length-1; i++)
        for(int j = i+1; j < unsortedArray.Length; j++)
            if(unsortedArray[i] > unsortedArray[j])
            {
                int temp = unsortedArray[i];
                unsortedArray[i] = unsortedArray[j];
                unsortedArray[j] = temp;
            }
    return unsortedArray;
}
public int[]BubbleSort(int[]unsortedArray)
{
for(int i=0;iunsortedArray[j])
{
int temp=未排序的数组[i];
unsortedArray[i]=unsortedArray[j];
未排序数组[j]=温度;
}
返回未排序的数组;
}
优化气泡排序:

public int[] BubbleSort(int[] unsortedArray)
{
    var n=unsortedArray.Length;
    while(n>0)
    {
        var newn=0;
        for(var i=1;i<=n-1;i++)
        {
            if(unsortedArray[i-1]>unsortedArray[i])
            {
                var temp = unsortedArray[i];
                unsortedArray[i] = unsortedArray[i-1];
                unsortedArray[i-1] = temp;
                newn=i;
            }
        }
        n=newn;
    }
}
public int[]BubbleSort(int[]unsortedArray)
{
var n=未排序的数组长度;
而(n>0)
{
var-newn=0;
对于(变量i=1;i传感器阵列[i])
{
var temp=未分类的数据[i];
unsortedArray[i]=unsortedArray[i-1];
未排序数组[i-1]=温度;
newn=i;
}
}
n=newn;
}
}
最好是:

public int[] BubbleSort(int[] unsortedArray)
{
    return unsortedArray.OrderBy(x=>x).ToArray();
}
原始版本的一些问题,
i
j
迭代太多了,它仍然可以工作,但它进行了不必要的迭代,不会影响结果,而且您的条件
unsortedArray[i]
是向后的

public int[] BubbleSort(int[] unsortedArray)
{
    for(int i = 0; i < unsortedArray.Length-1; i++)
        for(int j = i+1; j < unsortedArray.Length; j++)
            if(unsortedArray[i] > unsortedArray[j])
            {
                int temp = unsortedArray[i];
                unsortedArray[i] = unsortedArray[j];
                unsortedArray[j] = temp;
            }
    return unsortedArray;
}
public int[]BubbleSort(int[]unsortedArray)
{
for(int i=0;iunsortedArray[j])
{
int temp=未排序的数组[i];
unsortedArray[i]=unsortedArray[j];
未排序数组[j]=温度;
}
返回未排序的数组;
}
优化气泡排序:

public int[] BubbleSort(int[] unsortedArray)
{
    var n=unsortedArray.Length;
    while(n>0)
    {
        var newn=0;
        for(var i=1;i<=n-1;i++)
        {
            if(unsortedArray[i-1]>unsortedArray[i])
            {
                var temp = unsortedArray[i];
                unsortedArray[i] = unsortedArray[i-1];
                unsortedArray[i-1] = temp;
                newn=i;
            }
        }
        n=newn;
    }
}
public int[]BubbleSort(int[]unsortedArray)
{
var n=未排序的数组长度;
而(n>0)
{
var-newn=0;
对于(变量i=1;i传感器阵列[i])
{
var temp=未分类的数据[i];
unsortedArray[i]=unsortedArray[i-1];
未排序数组[i-1]=温度;
newn=i;
}
}
n=newn;
}
}
int[]bd=新的int[]{25,35,104,30,89,30,42,11,8,4,55,65,98,542,2};
对于(int rnd=bd.Length;rnd>0;rnd--)
{
对于(int i=bd.Length-1;i>=0;i--)
{
如果(i!=0)
{
if(bd[i-1]int[]bd=new int[]{25,35,104,30,89,30,42,11,8,4,55,65,98,542,2};
对于(int rnd=bd.Length;rnd>0;rnd--)
{
对于(int i=bd.Length-1;i>=0;i--)
{
如果(i!=0)
{
if(bd[i-1]对于(int j=0;j,如果
i
小于
j
,则用j替换i,也就是更高的数字。我猜说
var sorted=unsorteddarray.OrderBy(x=>x)是欺骗
?我哑口无言,有几分钟无法打字。如果你写了一个对数字数组进行反向排序的代码,那么你和我们都清楚地看到,你在某个地方犯了一个简单的错误,比如
,因为你在对它进行反向排序(从最高到最低)。在
if
块中反转测试(
if(unsortedArray[j]>unsortedArray[i])
@KenWhite,你反转了两次。你切换了条件,你切换了j/iIf
i
小于
j
,你用j替换i,也就是更高的数字。