Gooooogle • 2年前
1 2 3
int main() {
int a[10000], n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", & a[i]);
}
int i, j, itemp, gap = n;
while (gap > 1)
{
gap = gap / 2; /*增量缩小,每次减半*/
for ( i = gap; i < n; i = i+gap)
if (a[i] < a[i - gap]) {
j = i - gap;
itemp = a[i];
for (; itemp < a[j] && j > -1; j -= gap) {
a[j + gap] = a[j];
}
a[j + gap] = itemp;
}
}
评论:
int main() {
int a[10000], n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", & a[i]);
}
int i, j, itemp, gap = n;
while (gap > 1)
{
gap = gap / 2; /*增量缩小,每次减半*/
for ( i = gap; i < n; i = i+gap)
if (a[i] < a[i - gap]) {
j = i - gap;
itemp = a[i];
for (; itemp < a[j] && j > -1; j -= gap) {
a[j + gap] = a[j];
}
a[j + gap] = itemp;
}
}
for (int k = 0; k < n; k++) {
printf("%d ", a[k]);
}
return 0;
}
请先登录,才能进行评论