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
| #include <iostream>
using namespace std;
void mySwap(int &a,int &b){
int tmp = a;
a = b;
b = tmp;
}
void bubbleSort(int n,int a[]){
for (int i=0; i<n; i++) {
for (int j = 0; j<n-i-1; j++) {
if (a[j]<a[j+1]) {
mySwap(a[j], a[j+1]);
}
}
}
}
int main(int argc, const char * argv[]) {
int a[] = {2,7,4,1,8,9,6,5};
bubbleSort(8, a);
for (int i=0; i<8; i++) {
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
|