排序是指将一个无序序列按某个规则进行有序排序,而冒泡排序是排序算法中最基础的一种。现给一个序列a,其中元素个数为n,要求它们从小到大的顺序排序
冒泡排序的本质在于交换,即每次通过交换的方式把当前剩余元素的最大值移动到一端,而当剩余元素减少到0时,排序结束。
include
using namespace std;
int main()
{
int a[5] = { 4,3,5,2,1 };
int i = sizeof(a)/sizeof(int);
for(i--;i>0;i--)
{
for (int j = 0;j<i;j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
for (int k=4;k>=0;k--)
{
cout << a[k];
cout << endl;
}
system("pause");
return 0;
}