-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
42 lines (40 loc) · 882 Bytes
/
Copy pathBubbleSort.cpp
File metadata and controls
42 lines (40 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;
#define Maxsize 100
void BubbleSort(int r[],int n) //冒泡排序
{
int i,j,temp;
bool flag;
i=n-1;
flag=true;
while(i>0&&flag)
{
flag=false;
for(j=0;j<i;j++) //进行一趟排序
if(r[j]>r[j+1])
{
flag=true;
temp=r[j]; //交换两个记录
r[j]=r[j+1];
r[j+1]=temp;
}
for(j=0;j<=i;j++)//测试每趟排序结果
cout<<r[j]<<" ";
cout<<endl;
i--;
}
}
int main()
{
int i,n,r[Maxsize];
cout<<"请输入数列中的元素个数n为:"<<endl;
cin>>n;
cout<<"请依次输入数列中的元素:"<<endl;
for(i=0;i<n;i++)
cin>>r[i];
BubbleSort(r,n);
cout<<"冒泡排序结果:"<<endl;
for(i=0;i<n;i++)
cout<<r[i]<<" ";
return 0;
}