/*冒泡排序.cpp---study for the template*/
#include<iostream>
using namespace std;
template <typename ElementType>//关键
/*ElementType max(ElementType a, ElementType b){
return a > b ? a : b;
}*/
void SortBuble(ElementType* a,int size){//from little to big;
int work = 0;
ElementType temp;
for (int i = 0; i < size; i++){
for (int j = size - 1; j>=i; j--){
if (a[j] < a[j - 1]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
work = 1;
}
}
if (work == 0) break;
}
}
int main(){
cout.setf(ios_base::showpoint);
cout.precision(3);
/*cout << "max=" << max(5.0, 6.0)<<endl;
cout << "max=" << max(5, 6) << endl;
cout << "max=" << max('a', 'b') << endl;*/
int a = 3;//不用中间量交换两值
int b = 4;
cout << "a=" << a << "b=" << b << endl;
a = a - b;
b = a + b;
a = b - a;
cout << "a=" << a << "b=" << b << endl;
double s[10] = { 2.2, 3.5, 5.2, 6.25, 4.2, 8, 7.7, 2.8, 1, 15 };
SortBuble(s, 10);
for (int i = 0; i < 10; i++){ cout << s[i] << endl; }
system("pause");
return 0;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。