Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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# 泡沫排序背后的逻辑_C#_Bubble Sort - Fatal编程技术网

C# 泡沫排序背后的逻辑

C# 泡沫排序背后的逻辑,c#,bubble-sort,C#,Bubble Sort,我正在做一个泡泡式的练习,我的感觉是它非常接近正确。 就在此刻,我被呈现出一个永恒的循环 错在哪里 static void Main(string[] args) { int[] numbers = { 2, 4, 8, 5, 88, 55, 32, 55, 47, 8, 99, 120, 4, 32 }; int temporary; bool sorted; do { sorted

我正在做一个泡泡式的练习,我的感觉是它非常接近正确。 就在此刻,我被呈现出一个永恒的循环

错在哪里

static void Main(string[] args)
    {
        int[] numbers = { 2, 4, 8, 5, 88, 55, 32, 55, 47, 8, 99, 120, 4, 32 };
        int temporary;
        bool sorted;
        do
        {
            sorted = false;

            for (int i = 0; i < numbers.Length - 1; i++)
            {
                int a = numbers[i];
                int b = numbers[i + 1];
                if (a > b)
                {
                    temporary = a;
                    a = b;
                    b = temporary;

                    sorted = true;

                }
            }
            Console.WriteLine("sorted");
        } while (sorted == true);


        foreach (int i in numbers)
        {
            Console.Write(i + " ");
        }

    }
static void Main(字符串[]args)
{
int[]数字={2,4,8,5,88,55,32,55,47,8,99,120,4,32};
int临时;
布尔排序;
做
{
排序=假;
for(int i=0;ib)
{
临时=a;
a=b;
b=临时;
排序=真;
}
}
控制台。写入线(“排序”);
}while(排序==true);
foreach(整数i)
{
控制台。写入(i+“”);
}
}

您没有将结果写回数组

改用这个:

//temporary = a;
//a = b;
//b = temporary;

numbers[i] = b;
numbers[i + 1] = a;
应该是

if (numbers[i] > numbers[i + 1])
    {
            temporary = numbers[i];
            numbers[i] = numbers[i + 1];
            numbers[i + 1] = temporary;

            sorted = true;

     }
a
b
中所做的更改并不反映在
数字[i]
数字[i+1]
中,因为a和b仅仅是
数字[i]
副本

public void BubbleSort<T>(IList<T> list);
{
    BubbleSort<T>(list, Comparer<T>.Default);
}

public void BubbleSortImproved<T>(IList<T> list, IComparer<T> comparer)
{
    bool stillGoing = true;
    int k = 0;
    while (stillGoing)
    {
        stillGoing = false;
        for (int i = 0; i < list.Count - 1 - k; i++)
        {
            T x = list[i];
            T y = list[i + 1];
            if (comparer.Compare(x, y) > 0)
            {
                list[i] = y;
                list[i + 1] = x;
                stillGoing = true;
            }
        }
        k++;
    }
}
publicsvoidbubblesort(IList列表);
{
BubbleSort(列表,Comparer.Default);
}
公共无效气泡或改进(IList列表、IComparer比较器)
{
bool-stillGoing=true;
int k=0;
虽然(仍在进行)
{
仍然继续=错误;
for(int i=0;i0)
{
列表[i]=y;
列表[i+1]=x;
仍然继续=正确;
}
}
k++;
}
}
Jon Skeet在他的文章中对该算法进行了简要说明。它使用任意比较器,但允许您忽略它,在这种情况下,默认比较器用于相关类型。它将对IList的任何(非只读)实现(包括数组)进行排序


我希望这会有所帮助。

您可以将
a
b
交换,但您不需要对输入数组做任何操作。因此,您不断地在内存中交换值,但原始数组不变。尝试:

            for ( int i = 0; i < numbers.Length - 1; i++ )
            {
                if ( numbers[i] > numbers[i + 1] )
                {
                    temporary = numbers[i];
                    numbers[i] = numbers[i + 1];
                    numbers[i + 1] = temporary;

                    sorted = true;

                }
            }
for(int i=0;i数字[i+1])
{
临时=数字[i];
数字[i]=数字[i+1];
编号[i+1]=临时;
排序=真;
}
}

以下是一个工作示例:

static void Main(string[] args)
{
    int[] numbers = { 2, 4, 8, 5, 88, 55, 32, 55, 47, 8, 99, 120, 4, 32 };
    int temporary;
    bool sorted;

    do
    {
        sorted = false;
        for (int i = 0; i < numbers.Length - 1; i++)
        {
            if (numbers[i] > numbers[i + 1])
            {

                temporary = numbers[i];
                numbers[i] = numbers[i + 1];
                numbers[i + 1] = temporary;

                sorted = true;
            }
        }
        Console.WriteLine("sorted");
    } while (sorted == true);

    foreach (int i in numbers)
    {
        Console.Write(i + " ");
    }
}
static void Main(字符串[]args)
{
int[]数字={2,4,8,5,88,55,32,55,47,8,99,120,4,32};
int临时;
布尔排序;
做
{
排序=假;
for(int i=0;i数字[i+1])
{
临时=数字[i];
数字[i]=数字[i+1];
编号[i+1]=临时;
排序=真;
}
}
控制台。写入线(“排序”);
}while(排序==true);
foreach(整数i)
{
控制台。写入(i+“”);
}
}

@Killercam我想问题表明这是一种竞赛条件。酷。向所有人道歉……:'[您的循环总是在每次迭代中运行1到多次(以及该堆栈)。每次迭代都需要将结束条件减少1。因此您应该编写变量(具有number.length)在条件write i<'variable'--中。此外,您没有将结果保存在数组中。如果我可以添加,请检查值类型和引用类型之间的差异。+1表示泛型,但是!算法浪费时间检查已完全排序的'end'对象。Bubble称为'Bubble',因为他始终推送最大的数字到最右端。因此无需在每次迭代中反复检查。将“Length”存储到变量中,然后--将其保存在条件中。请检查's algorithm.Cool.Edited。谢谢您的时间。