#include <stdio.h>
void bubble_sort(int* arr, int sz)
{
//确定冒泡函数的趟数
int i = 0;
for (i = 0; i < sz - 1; i++)
{
//每一趟冒泡排序
int j = 0;
int flag = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = 0;
tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
flag = 0;//本趟排序数据不完全有序
}
}
if (flag==1)
{
break;
}
}
}
int main()
{
int arr[] = { 0,1,2,4,56,8,90,1,3 };
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr,sz);
//arr是数组,所以传参是传首元素的地址&arr[0]
printf("arr[bubble]=");
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
标签:tmp,sz,arr,int,冒泡排序,++,flag
From: https://blog.51cto.com/u_16168351/6588548