C语言实现冒泡排序

#include <stdio.h>

void bubble_sort(int arr[], int len) {

int i, j, temp;

for (i = 0; i < len - 1; i++) {

for (j = 0; j < len - 1 - i; j++) {

if (arr[j] > arr[j + 1]) {

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

}

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int len = (int) sizeof(arr) / sizeof(*arr);

bubble_sort(arr, len);

printf("Sorted array: ");

for (int i = 0; i < len; i++) {

printf("%d ", arr[i]);

}

printf("\n");

return 0;

}

相关推荐
闪电麦坤951 小时前
Leecode热题100:缺失的第一个正数(数组)
数据结构·算法·leetcode
梨子串桃子_8 小时前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
zfxwasaboy8 小时前
DRM KMS 子系统(4)Planes/Encoder/Connector
linux·c语言
夏鹏今天学习了吗8 小时前
【LeetCode热题100(83/100)】最长递增子序列
算法·leetcode·职场和发展
情缘晓梦.9 小时前
C语言指针进阶
java·开发语言·算法
极客代码9 小时前
深入解析C语言中的函数指针:原理、规则与实践
c语言·开发语言·指针·状态机·函数·函数指针
敲皮裤的代码9 小时前
《C语言》分支和循环(下)
c语言
北邮刘老师9 小时前
智能体治理:人工智能时代信息化系统的全新挑战与课题
大数据·人工智能·算法·机器学习·智能体互联网
AlenTech10 小时前
155. 最小栈 - 力扣(LeetCode)
算法·leetcode·职场和发展
mit6.82410 小时前
正反两次扫描|单调性cut
算法