什么是最优雅的方式来冒泡排序在C#中?优雅、方式

由网友(空城旧梦)分享简介:可以这样被清理?使用系统;类AscendingBubbleSort{公共静态无效的主要(){INT I = 0,J = 0,t = 0时;INT [] C =新INT [20];对于(I = 0; I&小于20;我+ +){Console.WriteLine(请输入值P [{0}]:我);C [i] = int.P...

可以这样被清理?

 使用系统;
类AscendingBubbleSort
{
    公共静态无效的主要()
    {
        INT I = 0,J = 0,t = 0时;
        INT [] C =新INT [20];
        对于(I = 0; I&小于20;我+ +)
        {
            Console.WriteLine(请输入值P [{0}]:我);
            C [i] = int.Parse(到Console.ReadLine());
        }
        //排序:冒泡排序
        对于(I = 0; I&小于20;我+ +)
        {
            为(J = + 1; J&小于20; J ++)
            {
                如果(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(泡沫排序的数组:);
        //排序阵列输出
        对于(I = 0; I&小于20;我+ +)
        {
            Console.WriteLine(C [{0}] = {1},I,C [I]);
        }
    }
}
 

解决方案

您已经粘贴有什么还没有一个泡沫排序。这是一种暴力之类的,但它不是冒泡排序。这里有一个通用的冒泡排序的例子。它利用一个任意的比较器,但是可以让你忽略它在这种情况下默认的比较用于相关类型。它将排序的任何(非只读)实施中的IList< T> ,其中包括阵列。看了上面的链接(维基百科),以获得更多的冒泡排序是如何为了工作的想法。请注意如何在每个循环中,我们经历从开始到结束,而只是与它的邻居比较每个项目。它仍然是一个为O​​(n 2 )排序算法,但在许多情况下,它会比你给的版本速度更快。

 公共无效冒泡< T>(IList的< T>清单);
{
    冒泡< T>(列表的Comparer< T> .DEFAULT);
}

公共无效冒泡< T>(IList的< T>清单,IComparer的< T>比较器)
{
    布尔stillGoing = TRUE;
    而(stillGoing)
    {
        stillGoing = FALSE;
        的for(int i = 0; I< list.Length-1;我++)
        {
            牛逼X =列表[我]
            牛逼Y =列表[我+ 1];
            如果(comparer.Compare(X,Y)大于0)
            {
                名单[I] = Y;
                名单[I + 1] = X;
                stillGoing = TRUE;
            }
        }
    }
}
 
冒泡排序的解决方式

Can this be cleaned up?

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

解决方案

What you've pasted there isn't a bubble sort. It's a sort of "brute force" sort but it's not bubble sort. Here's an example of a generic bubble sort. It uses an arbitrary comparer, but lets you omit it in which case the default comparer is used for the relevant type. It will sort any (non-readonly) implementation of IList<T>, which includes arrays. Read the above link (to Wikipedia) to get more of an idea of how bubble sort is meant to work. Note how on each loop we go through from start to finish, but only compare each item with its neighbour. It's still an O(n2) sort algorithm, but in many cases it will be quicker than the version you've given.

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

阅读全文

相关推荐

最新文章