1.8冒泡排序

发布时间 2023-04-19 19:00:31作者: 啵菠菜

1.问题描述

把N个整数按照升序排列

2.问题分析。

利用数组储存,然后相邻数组进行排序。

3.代码

#include <iostream>
#include <string>
using namespace std;

int main() {
int n[10];
int i, j;
int temp; 
cout << "请输入十个数字!" << endl;
for (i = 0; i < 10; i++) {
cin >> n[i];
}
for (i = 0; i < 9;i++) { 
for (j = 0; j < 9 - i;j++) {
if (n[j] > n[j + 1]) {
temp = n[j];
n[j] = n[j + 1];
n[j + 1] = temp;
}
}
}
cout << "排序后的数据是:" << endl;
for (i = 0; i < 10; i++)
{
cout << n[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}