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

#include <stdio.h>

void bubble_sort(int arr[], int n) {
    int i, j;
    for (i = 0; i < n - 1; i++) {
        for (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;
            }
        }
    }
}

int main() {
    int arr[] = {5, 2, 8, 3, 1, 6};
    int n = sizeof(arr) / sizeof(arr[0]);
    bubble_sort(arr, n);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}

其中,bubble_sort函数实现了冒泡排序,main函数则演示了如何使用该函数对一个数组进行排序。

C语言-冒泡法排序-代码

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

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