5

这个可以清理吗?

using System;  
class AscendingBubbleSort 
{     
    public static void Main()
    {
        int i = 0,j = 0,t = 0;
        int []c=new int[20];
        for(i=0;i<20;i++)
        {
            Console.WriteLine("Enter Value p[{0}]:", i);
            c[i]=int.Parse(Console.ReadLine());
        }
        // Sorting: Bubble Sort
        for(i=0;i<20;i++)
        {
            for(j=i+1;j<20;j++)
            {
                if(c[i]>c[j])
                {
                    Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
                    t=c[i];
                    c[i]=c[j];
                    c[j]=t;
                }
            }
        }
        Console.WriteLine("bubble sorted array:");
        // sorted array output
        for(i=0;i<20;i++)
        {
            Console.WriteLine ("c[{0}]={1}", i, c[i]);
        }
    }
}
4

10 回答 10

35

您粘贴的内容不是冒泡排序。这是一种“蛮力”排序,但不是冒泡排序。这是一个通用冒泡排序的示例。它使用任意比较器,但允许您省略它,在这种情况下,默认比较器用于相关类型。它将对 的任何(非只读)实现进行排序IList<T>,其中包括数组。阅读上面的链接(维基百科),以更多地了解冒泡排序的工作原理。请注意我们在每个循环中如何从头到尾进行,但仅将每个项目与其邻居进行比较。它仍然是一个 O(n 2 ) 排序算法,但在许多情况下它会比您给出的版本更快。

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

public void BubbleSort<T>(IList<T> list, IComparer<T> comparer)
{
    bool stillGoing = true;
    while (stillGoing)
    {
        stillGoing = false;
        for (int i = 0; i < list.Count-1; 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;
            }
        }
    }
}
于 2009-10-20T15:07:14.697 回答
13

C# 中最优雅的排序方式是

Array.Sort( object[] )

除了老师要求您实施非优雅冒泡排序算法的家庭作业问题外,这将在任何地方都有效。;-)

于 2009-10-20T15:04:53.283 回答
8

总的来说,您的冒泡排序实现没有任何问题。如果我进行真正的代码审查,我会做出以下更改:

选择更具描述性的变量名称

为什么你的数组刚刚被调用c

最小化变量范围

您的所有变量都在函数顶部声明。除非这是家庭作业要求或编码标准,否则将变量声明为“靠近”使用它们的位置更为惯用,最好使它们具有尽可能小的范围。

因此,删除读取的第一行int i = 0,j = 0,t = 0;。内联声明循环计数器:

for(int i = 0; i < 20; i++)

并在它使用的地方声明你的临时变量:

                Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
                int t=c[i];
                c[i]=c[j];
                c[j]=t;

消除硬编码的数组边界。

这:

for(i=0;i<20;i++)

变成这样:

for(i = 0; i < c.Length; i++)
于 2009-10-20T15:08:33.133 回答
3

大多数人不会费心使冒泡排序变得优雅。不过,总的来说,我发现这样做:

for (int i = 0; i < items.Length; i++) {
    Item item = items[i];
    // do something with item
}

比这样做更优雅,更易于维护:

Item item;
int i;
for (i = 0; i < items.Length; i++) {
    item = items[i];
    // do something with item
}

换句话说,在最小的适用范围内声明您的变量i否则,您可能会发现自己在代码中的某个点或其他点做某事item,然后在不应该出现的地方再次使用它们。

于 2009-10-20T15:08:04.907 回答
2
  • 我会使用交换方法来交换两个数组项。(关于如何编写交换方法的细节留作作业!)

  • 您应该考虑物品已经有序的情况

  • 您应该阅读插入排序以获得更多标记:-)

  • 而不是从键盘读取测试数据,看看你是否可以学习如何使用nUnit

于 2009-10-20T15:05:56.447 回答
1

我个人更喜欢这个:

string foo [] = new string[] {"abc", "def", "aaa", "feaf", "afea" };
Array.Sort(foo);

但这只是我。排序是一个已解决的问题,为什么要重新发明轮子?

于 2009-10-20T14:59:49.930 回答
1

我相信Jon Skeet提出的答案有所改进。每次循环后,迭代次数应排除上一次迭代中处理的最后一项。所以,这里是代码:

public void BubbleSortImproved<T>(IList<T> list)
{
    BubbleSortImproved<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;
        //reduce the iterations number after each loop
        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++;
    }
}
于 2012-12-06T10:57:58.673 回答
1
int[] array = {4,5,7,1,8};           

int n1, n2;
bool stillgoing = true;

while (stillgoing)
{
    stillgoing = false;
    for (int i = 0; i < (array.Length-1); i++)
    {                  
        if (array[i] > array[i + 1])
        {
            n1 = array[i + 1];
            n2 = array[i];

            array[i] = n1;
            array[i + 1] = n2;
            stillgoing = true; 
        }
    }
}
for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine(array[i]);
}

从 Jon skeet 那里得到了一些想法......

于 2013-03-02T15:55:19.380 回答
1
    public int[] BubbleSortInAesc(int[] input)
    {
        for (int i = input.Length; i > 0; i--)
        {
            for (int j = 0; j < i-1; j++)
            {
                if (input[j] > input[j + 1])
                {
                    //Swap the numbers
                    input[j] = input[j + 1]+input[j];
                    input[j + 1] = input[j] - input[j + 1];
                    input[j] = input[j] - input[j + 1];
                }
            }
        }
        return input;
    }
于 2013-03-18T04:29:38.510 回答
0

我认为您的算法没问题,但我会将排序功能放在单独的类和方法中。

于 2009-10-20T15:01:25.637 回答