void bubble_sort_1(int a[], int len)
{
int i = 0;
int j = 0;
int temp = 0;
for(i = 0; i < len - 1; i++)
{
for(j = len - 1; j >= i; j--) //小的冒到开头部分
{
if(a[j + 1] < a[j])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
//冒泡法的改进
void bubble_sort_2(int a[], int len)
{
int i = 0;
int j = 0;
int temp = 0;
int exchange = 0; //记录每次扫描是否交换
for(i = 0; i < len - 1; i++)
{
exchange = 0;
for(j = len - 1; j >=i; j--) //小的冒到开头部分
{
if(a[j + 1] < a[j])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
exchange = 1;
}
}
if(exchange != 1)
return;
}
}