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

using System;

public class BubbleSort {
    public static void Main() {
        int[] arr = { 64, 34, 25, 12, 22, 11, 90 };
        int n = arr.Length;
        Sort(arr, n);
        Console.WriteLine("排序后的数组:");
        PrintArray(arr, n);
    }

    static void Sort(int[] arr, int n) {
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    static void PrintArray(int[] arr, int n) {
        for (int i = 0; i < n; ++i) {
            Console.Write(arr[i] + " ");
        }
        Console.WriteLine();
    }
}

以上代码中,Sort()方法使用嵌套循环进行冒泡排序,PrintArray()方法用于打印排序后的数组。在Main()方法中,我们定义了一个整型数组,并调用Sort()方法进行排序,最后调用PrintArray()方法打印结果

给我写个C#的冒泡排序

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

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