64,648
社区成员
发帖
与我相关
我的任务
分享
void InsertSorts(int a[], int n)
{
for(int i = 1; i != n; ++i)
{
int temp = a[i];
for(int j = i - 1; j >= 0; --j)
{
if(a[j] > temp)
{
a[j + 1] = a[j];
}
}
a[j] = temp;
}
}
void InsertSortss(int a[], int n)
{
for(int i = 1; i != n; ++i)
{
int j = i - 1;
int temp = a[i];
while(a[j] > temp)
{
a[j + 1] = a[j];
--j;
}
a[j + 1] = temp;
}
}
为什么这样写就是对的,感觉两个逻辑是一样的
void InsertSorts(int a[], int n)
{
for(int i = 1; i != n; ++i)
{
int temp = a[i];
for(int j = i - 1; j >= 0; --j)
{
if(a[j] > temp)
{
a[j + 1] = a[j];
}
else
{
break;
}
}
a[j+1] = temp;
}
}
void InsertSorts(int a[], int n)
{
for(int i = 1; i != n; ++i)
{
int temp = a[i];
for(int j = i - 1; j >= 0; --j)
{
if(a[j] > temp)
{
a[j + 1] = a[j];
}
else
{
break;
}
}
a[j] = temp;
}
}