代碼:
#include <iostream>
using namespace std;
int main()
{
/*
冒泡排序
*/
int arr[5] = { -1,3,1,10,5 };
int length = sizeof(arr) / sizeof(arr[0]);//5
int count = length - 1;//4
int temp;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < (count - i);j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
/*
打印排序後的結果
*/
for (int a = 0; a < length; a++)
{
cout << arr[a] << " ";
}
cout << endl;
system("pause");
return 0;
}
結果: