#include <stdio.h>
void bubbleSort(int arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - 1 - i; j++) // 每次将最大值放到最后所以会少i
{
if (arr[j] > arr[j + 1])
{
// 交换arr[j]和arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int arr[] = {3, 1, 5, 4, 2};
int size = sizeof(arr) / sizeof(arr[0]);
printf("原始数组:");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
bubbleSort(arr, size);
printf("排序后的数组:");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
标签:arr,int,程序,冒泡排序,bubbleSort,++,printf,size
From: https://blog.csdn.net/2301_77228514/article/details/139839588