以下是C#的冒泡排序示例代码:

public static void BubbleSort(int[] array)
{
    int temp;
    for (int i = 0; i < array.Length - 1; i++)
    {
        for (int j = 0; j < array.Length - i - 1; j++)
        {
            if (array[j] > array[j + 1])
            {
                temp = array[j + 1];
                array[j + 1] = array[j];
                array[j] = temp;
            }
        }
    }
}

使用示例:

int[] array = { 5, 1, 4, 2, 8 };
BubbleSort(array);
foreach (int i in array)
{
    Console.Write(i + " ");
}
// 输出:1 2 4 5 8
``
写一个C#的冒泡排序

原文地址: https://www.cveoy.top/t/topic/ePX5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录