东方博宜OJ 1010:数组元素的排序 ← 冒泡排序

【题目来源】
https://oj.czos.cn/p/1010

【题目描述】
对数组的元素按从小到大进行排序。

【输入格式】
第一行有一个整数 n(5≤n≤10);
第二行有 n 个整数,每个整数的值在 [0, 10^9]的范围内。

【输出格式】
输出排序后的数组。

【输入样例】
8
1 2 3 6 8 7 4 5

【输出样例】
1 2 3 4 5 6 7 8

【数据范围】
5≤n≤10

【算法分析】
● 冒泡排序是一种最简单的交换排序方法,它通过两两比较相邻记录的关键字,如果为逆序,则进行交换,从而使关键字小的记录如气泡一般逐渐往上“漂浮”,或者使关键字大的记录如石块一样逐渐向下“坠落”。
● 直接插入排序:https://blog.csdn.net/hnjzsyjyj/article/details/161332702
● 选择排序:https://blog.csdn.net/hnjzsyjyj/article/details/161346075

【算法代码:冒泡排序

#include <bits/stdc++.h>
using namespace std;

const int N=15;
int a[N];
int n;

int main() {
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i];
    }

    for(int i=1; i<n; i++) {
        for(int j=1; j<=n-i; j++) {
            if(a[j]>a[j+1]) {
                swap(a[j],a[j+1]);
            }
        }
    }

    for(int i=1; i<=n; i++) {
        cout<<a[i]<<" ";
    }

    return 0;
}

/*
in:
5
6 9 2 7 1

out:
1 2 6 7 9
*/




【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/121733509
https://www.luogu.com.cn/problem/solution/P7910

posted @ 2026-05-23 23:02  Triwa  阅读(12)  评论(0)    收藏  举报