#include <stdio.h>
#include <windows.h>
void BubbleSort(int * arr, int n);
void BubbleSort2(int * arr, int n);
int main(void)
{
int arr[] = {8, 5, 2, 6, 4, 3, 7, 1};
int n = sizeof(arr)/sizeof(int);
BubbleSort(arr, n);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
system("pause");
return 0;
}
void BubbleSort(int * arr, int n)
{
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - 1 - i; j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void BubbleSort2(int * arr, int n)
{
bool flag = true;
for(int i = 0; i < n - 1 && flag; i++)
{
flag = false;
for(int j = n - 2; j >= i; j--)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = true;
}
}
}
}
09-23
116
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-14
199
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-04
1929
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)