欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

C语言·冒泡排序

时间:2023-05-29
C语言·冒泡排序

前言说明实现思路流程图冒泡排序·代码·有注释冒泡排序·代码结语 前言说明

将一串自己输入的数字,通过冒泡排序,由小到大进行输出。

实现思路

将第一个数与第二个数比较;满足条件就换位;将第二个数与第三个数比较;满足条件就换位;将第三个数与。。。。。。(以此类推)

流程图

暂无

冒泡排序·代码·有注释

#include #define value_1 5int main(int argc, char* argv[]){int clas_1[value_1];int value_2 = 0;int value_3 = 0;int i;printf("请输入%d个数:", value_1);for (i = 0; i < value_1; i++){scanf_s("%d", &clas_1[i]);}printf("nn您输入的数是:");for (i = 0; i < value_1; i++){printf("%d", clas_1[i]);if (i < value_1 - 1){printf(",");}}i = 0;for (value_2 = 0; value_2 < (value_1 - 1); value_2++){for (value_3 = 0; value_3 < ((value_1 - 1) - value_2); value_3++){if (clas_1[value_3] > clas_1[value_3 + 1]){i = clas_1[value_3];clas_1[value_3] = clas_1[value_3 + 1];clas_1[value_3 + 1] = i;}}}printf("nn排序后的结果是:");for (i = 0; i < value_1; i++){printf("%d", clas_1[i]);if (i < value_1 - 1){printf("<");}}printf("n");return 0;}

冒泡排序·代码

#include #define value_1 5int main(int argc, char* argv[]){int clas_1[value_1];int value_2 = 0;int value_3 = 0;int i;printf("请输入%d个数:", value_1);for (i = 0; i < value_1; i++){scanf_s("%d", &clas_1[i]);}printf("nn您输入的数是:");for (i = 0; i < value_1; i++){printf("%d", clas_1[i]);if (i < value_1 - 1){printf(",");}}i = 0;for (value_2 = 0; value_2 < (value_1 - 1); value_2++){for (value_3 = 0; value_3 < ((value_1 - 1) - value_2); value_3++){if (clas_1[value_3] > clas_1[value_3 + 1]){i = clas_1[value_3];clas_1[value_3] = clas_1[value_3 + 1];clas_1[value_3 + 1] = i;}}}printf("nn排序后的结果是:");for (i = 0; i < value_1; i++){printf("%d", clas_1[i]);if (i < value_1 - 1){printf("<");}}printf("n");return 0;}

结语

编程新手。此代码有错误的或者有意见的,欢迎致信!
QQ邮箱:2787485199@qq.com

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。