参考文章1: https://www.cnblogs.com/mistermoney/p/9550590.html
基础写法: 比写法b稍好
int bubble1(int test[], int length){
int temp = 0;
int count = 0;
for(int i = 0; i< length-1;i++){
for(int j = i+1; j< 10;j++){
if(test[j]<test[i]){
temp = test[i];
test[i] = test[j];
test[j] = temp;
}
cout<<" i = "<<i<<" j = "<<j<<" : ";
print_shuzhu(10,test);
count++;
}
cout<<"-------------------------------------------------------total count= "<<count<<endl;
}
return 0;
}
基础写法b:
int bubble2(int test[], int length){
int temp = 0;
int count = 0;
for(int i = 0; i< length;i++){
for(int j = 0; j< length-i-1;j++){
if(test[j]>test[j+1]){
temp = test[j];
test[j] = test[j+1];
test[j+1] = temp;
//cout<< " switching ======================";
}
//cout<<" i = "<<i<<" j = "<<j<<" : ";
//print_shuzhu(length,test);
count++;
}
//cout<<"-------------------------------------------------------total count= "<<count<<endl;
}
return 0;
}
优化1: 外循环优化版本
int bubble22(int test[], int length){
int temp = 0;
int count = 0;
int flag = 1;
for(int i = 0; i< length;i++){
flag = 1;
for(int j = 0; j< length-i-1;j++){
if(test[j]>test[j+1]){
temp = test[j];
test[j] = test[j+1];
test[j+1] = temp;
//cout<< " switching ======================";
flag = 0;
}
//cout<<" i = "<<i<<" j = "<<j<<" : ";
//print_shuzhu(length,test);
count++;
}
//cout<<"-------------------------------------------------------total count= "<<count<<endl;
if(flag == 1){
break;
}
}
return 0;
}
优化2: 外循环+内循环优化版本 性能最优版本
int bubble222(int test[], int length){
int temp = 0;
int count = 0;
int flag = 1;
int len = length -1;
int pos = 0;
for(int i = 0; i< length;i++){
flag = 1;
for(int j = 0; j< len;j++){
if(test[j]>test[j+1]){
temp = test[j];
test[j] = test[j+1];
test[j+1] = temp;
//cout<< " switching ======================";
flag = 0;
pos = j;
}
//cout<<" i = "<<i<<" j = "<<j<<" : ";
//print_shuzhu(length,test);
count++;
}
len = pos;
//cout<<"-------------------------------------------------------total count= "<<count<<endl;
if(flag == 1){
break;
}
}
//print_shuzhu(length,test);
return 0;
}
优化变异版本: 双向冒泡排序
int bubble3(int test[], int length){
int temp = 0;
int count = 0;
int flag = 1;
int len = length -1;
int pos = 0;
int pos_l = 0;
int len_l = 0;
for(int i = 0; i< length;i++){
flag = 1;
for(int j = len_l; j< len;j++){
if(test[j]>test[j+1]){
temp = test[j];
test[j] = test[j+1];
test[j+1] = temp;
//cout<< " switching ======================"<<endl;
flag = 0;
pos = j;
}
count++;
}
for(int j= len;j>len_l;j--){
if(test[j]<test[j-1]){
temp = test[j];
test[j] = test[j-1];
test[j-1] = temp;
//cout<< "2 switching ======================"<<endl;
flag = 0;
pos_l = j;
}
count++;
}
len = pos;
len_l = pos_l;
//cout<<" i = "<<i<<" : ";
//print_shuzhu(length,test);
//cout<<"-------------------------------------------------------total count= "<<count<<endl;
if(flag == 1){
break;
}
}
//print_shuzhu(length,test);
return 0;
}
双向排序写法2:
int bubble4(int test[], int length){
int temp = 0;
int count = 0;
int index_max = 0;
int max =0 ;
for(int i = 0; i< length-1;i++){
index_max = i+1;
max = test[i+1];
for(int j = i+1; j< length-i;j++){
if(test[j]<test[i]){
temp = test[i];
test[i] = test[j];
test[j] = temp;
if(max < test[j]){
max = test[j];
index_max = j;
}
}
if(index_max != i+1){
temp = test[index_max];
test[index_max] = test[length-i-1];
test[length-i-1] = temp;
}
//cout<<" i = "<<i<<" j = "<<j<<" : ";
//print_shuzhu(10,test);
count++;
}
//cout<<"-------------------------------------------------------total count= "<<count<<endl;
}
print_shuzhu(length,test);
return 0;
}
性能比较: 注意要循环比较多次后 取最后几遍的数据。 因为第一遍会设计很多内存分配等额外的工作,数据是不准的。
int main() {
int size = 5000;
int test[size];
srand(time(0));
for(int i =0;i< size;i++){
test[i] = rand()%60000;
}
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳
//bubble1(test,10);
//bubble2(test,10);
bubble3(test,size);
high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳
duration<double, std::milli> time_span = t2 - t1;
std::cout << time_span.count() << std::endl;
for(int j = 0; j< 6;j++){
cout<<"----------------------------------------------------"<<endl;
t1 = high_resolution_clock::now(); //返回时间戳
bubble1(test,size);
t2 = high_resolution_clock::now(); //返回时间戳
time_span = t2 - t1;
std::cout <<" 普通写法1: " <<time_span.count() << std::endl;
t1 = high_resolution_clock::now(); //返回时间戳
bubble2(test,size);
t2 = high_resolution_clock::now(); //返回时间戳
time_span = t2 - t1;
std::cout <<" 普通写法2: " <<time_span.count() << std::endl;
t1 = high_resolution_clock::now(); //返回时间戳
bubble22(test,size);
t2 = high_resolution_clock::now(); //返回时间戳
time_span = t2 - t1;
std::cout << "外循环优化 : "<<time_span.count() << std::endl;
t1 = high_resolution_clock::now(); //返回时间戳
bubble222(test,size);
t2 = high_resolution_clock::now(); //返回时间戳
time_span = t2 - t1;
std::cout << "内外循环优化 : "<<time_span.count() << std::endl;
t1 = high_resolution_clock::now(); //返回时间戳
bubble3(test,size);
t2 = high_resolution_clock::now(); //返回时间戳
time_span = t2 - t1;
std::cout << "双向排序法 : "<<time_span.count() << std::endl;
t1 = high_resolution_clock::now(); //返回时间戳
bubble4(test,size);
t2 = high_resolution_clock::now(); //返回时间戳
time_span = t2 - t1;
std::cout << "双向排序法2 : "<<time_span.count() << std::endl;
}
return 0;
}
结果如下:
----------------------------------------------------
普通写法1: 18.7907
普通写法2: 24.5693
外循环优化 : 0.009068
内外循环优化 : 0.008118
双向排序法 : 0.016226
双向排序法2 : 11.552
----------------------------------------------------
普通写法1: 19.3609
普通写法2: 25.3532
外循环优化 : 0.028983
内外循环优化 : 0.008537
双向排序法 : 0.017059
双向排序法2 : 12.3802
----------------------------------------------------
普通写法1: 18.8317
普通写法2: 23.9303
外循环优化 : 0.009566
内外循环优化 : 0.008385
双向排序法 : 0.016663
双向排序法2 : 11.7361