C语言实现冒泡排序
作者:互联网
void bubble_soft(int arr[], int sz)
{
int i, j;
int flag = 1;
for (i = 0; i<sz - 1; i++)
{
for (j = 0; j<sz - 1 - i; j++)
{
if (arr[j]>arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
flag = 0;
}
}
if (flag == 1)
{
break;
}
}
}
int main()
{
int arr[] = { 3, 1, 6, 4, 7, 8, 2, 9 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
bubble_soft(arr, sz);
for (i = 0; i<sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
标签:tmp,sz,arr,实现,冒泡排序,C语言,int,flag,bubble 来源: https://blog.csdn.net/m0_60603462/article/details/120102350