69,373
社区成员
发帖
与我相关
我的任务
分享
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
void sort(int a[], int n)
{
int i, j, t, tag=1; //以下代码的思想请一一解释!
for(j=0; j <n; j++)
{
for(i=j%2; i<n-1;i+=2)
{
if(a[i]>a[i+1])
{
t=a[i];a[i]=a[i+1]; a[i+1]=t;
tag=0;
}
}
tag++;
if(tag==2) break;
}
}
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
void sort(int a[], int n)
{
int i, j, t, tag=1; //以下代码的思想请一一解释!t为临时数据.tag为标签
for(j=0; j <n; j++)
{
for(i=j%2; i<n-1;i+=2)
{
if(a[i]>a[i+1])
{
t=a[i];a[i]=a[i+1]; a[i+1]=t;
tag=0;
}
}
tag++;
if(tag==2) break;
}
}
int main()
{
int array[8] = {6, 7, 8, 9, 0, 1, 2, 3};
sort(array, 8);
for (int i = 0; i < 8; ++i)
{
std::cout << array[i] << " ";
}
system("pause");
return 0;
}