如何在 C# 中创建通用冒泡排序

发布于 2024-09-28 00:40:51 字数 618 浏览 8 评论 0原文

我目前正在制作自己的通用冒泡排序,它可以轻松地对字符串、整数进行排序。下面是我的普通冒泡排序代码。你能帮我看看如何创建一个通用方法吗?

public static void BubbleSorting()
{

       int Swap;
        for (int outer = Length; outer >= 1; outer--)
        {
            for (int inner = 0; inner < outer - 1; inner++)
            {
                if (array[inner] > array[inner + 1])
                {
                    Swap = array[inner];
                    array[inner] = array[inner + 1];
                    array[inner + 1] = Swap;
                }

            }
            Console.WriteLine();
            Display();
        }

    }

I am currently working on making my own Generic Bubble Sorting which can easily sort Strings, int. Below is my Code for Normal Bubble Sorting.Can you help me out how to create a generic Method Of this?

public static void BubbleSorting()
{

       int Swap;
        for (int outer = Length; outer >= 1; outer--)
        {
            for (int inner = 0; inner < outer - 1; inner++)
            {
                if (array[inner] > array[inner + 1])
                {
                    Swap = array[inner];
                    array[inner] = array[inner + 1];
                    array[inner + 1] = Swap;
                }

            }
            Console.WriteLine();
            Display();
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难以启齿的温柔 2024-10-05 00:40:52

如果数组中的元素实现了 IComparable,则只需使用 IComparable 即可,您可以将其

array[inner] > array[inner + 1]

array[inner].CompareTo(array[inner + 1]) > 0

替换

public static void BubbleSorting<T>(T[] array) where T : Icomparable
{
   for (int outer = Length; outer >= 1; outer--)
    {
        for (int inner = 0; inner < outer - 1; inner++)
        {
            if (array[inner].CompareTo(array[inner + 1]) > 0)
            {
                T Swap = array[inner];
                array[inner] = array[inner + 1];
                array[inner + 1] = Swap;
            }

        }
    }
}

Just use IComparable if the elements in the array implements IComparable you can replace

array[inner] > array[inner + 1]

with

array[inner].CompareTo(array[inner + 1]) > 0

so end up with

public static void BubbleSorting<T>(T[] array) where T : Icomparable
{
   for (int outer = Length; outer >= 1; outer--)
    {
        for (int inner = 0; inner < outer - 1; inner++)
        {
            if (array[inner].CompareTo(array[inner + 1]) > 0)
            {
                T Swap = array[inner];
                array[inner] = array[inner + 1];
                array[inner + 1] = Swap;
            }

        }
    }
}
坏尐絯 2024-10-05 00:40:52
    public static void BubbleSort<T>(T[] list) 
    { 
        BubbleSort<T>(list, Comparer<T>.Default); 
    }

    public static void BubbleSort<T>(T[] list, IComparer<T> comparer)
    {
        bool KeepIterating = true;
        while (KeepIterating)
        {
            KeepIterating = 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;
                    KeepIterating = true;

                    for (int j = 0; j < list.Length; j++)
                    {
                        Console.WriteLine("{0} {1}",j,list[j]);
                    }
                }                    
            }
        }
    }
    public static void BubbleSort<T>(T[] list) 
    { 
        BubbleSort<T>(list, Comparer<T>.Default); 
    }

    public static void BubbleSort<T>(T[] list, IComparer<T> comparer)
    {
        bool KeepIterating = true;
        while (KeepIterating)
        {
            KeepIterating = 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;
                    KeepIterating = true;

                    for (int j = 0; j < list.Length; j++)
                    {
                        Console.WriteLine("{0} {1}",j,list[j]);
                    }
                }                    
            }
        }
    }
小矜持 2024-10-05 00:40:51
public static void BubbleSort<T>(T[] array, IComparer<T> comparer) {
  if (comparer == null) comparer = Comparer<T>.Default;
  T Swap;
  // etc..
}
public static void BubbleSort<T>(T[] array, IComparer<T> comparer) {
  if (comparer == null) comparer = Comparer<T>.Default;
  T Swap;
  // etc..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文