2

我正在做一个冒泡排序练习,我的感觉是它非常接近正确。就像现在一样,我看到了一个永恒的循环。

错在哪里?

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 + " ");
        }

    }
4

5 回答 5

4

C# 中更好的方法是使用通用冒泡排序

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

Jon Skeet 在他的回答中给出了这个算法的简要解释。“它使用任意比较器,但允许您省略它,在这种情况下,默认比较器用于相关类型。它将对 IList 的任何(非只读)实现进行排序,其中包括数组。”

我希望这有帮助。

于 2012-12-06T09:21:35.603 回答
3

你交换ab但你不对你的输入数组做任何事情。因此,您不断地在内存中交换值,但原始数组没有改变。尝试:

            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;

                }
            }
于 2012-12-06T09:21:39.577 回答
2

您没有将结果写回数组。

改用这个:

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

numbers[i] = b;
numbers[i + 1] = a;
于 2012-12-06T09:21:06.177 回答
1

它应该是

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

            sorted = true;

     }

中所做的更改ab不会反映在,numbers[i] numbers[i+1]因为 a 和 b 仅仅copynumbers[i] numbers[i+1]..

于 2012-12-06T09:21:17.533 回答
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 + " ");
    }
}
于 2012-12-06T09:25:33.487 回答