冒泡排序

xiaoxiao2025-11-02  2

#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 105; int n,s[maxn]; void bubblesort() { int flag = 1; for(int i = 0; flag; i++){ flag = 0; for(int j = n - 1; j >= i + 1; j--){ if(s[j] > s[j - 1]){ int t = s[j]; s[j] = s[j - 1]; s[j - 1] = t; flag = 1;//只要flag是1就证明有交换,就还要循环,直到某次循环不出现交换,即flag=0时就不会循环了 } } } } int main(void) { scanf("%d",&n); for(int i = 0; i < n; i++) scanf("%d",&s[i]); bubblesort(); for(int i = 0; i < n; i++) printf("%d ",s[i]); return 0; }

 

 

参考链接:https://blog.csdn.net/zhouzi2018/article/details/80288856

转载请注明原文地址: https://www.6miu.com/read-5038939.html

最新回复(0)