define _CRT_SECUNRE_NO_WARNINGS 1
include<stdio.h>
void bubble_sort(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)//冒泡的次数
{
int flag = 1;//假设这一趟排序已经有序
int j = 0;
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[] = { 10, 9, 8, 7, 6, 4, 5, 3, 2, 1 };
int i = 0;
int sz = sizeof arr / sizeof (arr[0]);
//对arr进行的传参,实际上传递过去的是数组arr的首元素地址&arr[0]
bubble_sort(arr, sz );
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}