OJ5-2 B. Shell sort

6-Tong發表於2020-11-11

在這裡插入圖片描述
在這裡插入圖片描述
輸入樣例文字版:
10
49,38,65,97,76,13,27,50,2,8,

#include <stdio.h>
#include <stdlib.h>
void shellSort(int a[], int n);
int main()
{
    int n;
    scanf("%d", &n);
    int a[n];
    for (int i = 0; i < n; i++)
    {
        scanf("%d,", &a[i]);
    }
    shellSort(a, n);
    return 0;
}

void shellSort(int a[], int n)
{
    int increment;
    int i, j;
    int temp;
    for (increment = n / 2; increment >= 1; increment = increment / 2)
    {
        for (i = increment; i < n; i++)
        {
            temp = a[i];
            for (j = i; j >= increment && temp > a[j - increment]; j = j - increment)
            {
                a[j] = a[j - increment];
            }
            a[j] = temp;
        }
        for (int k = 0; k < n; k++)
        {
            printf("%d,", a[k]);
        }
        printf("\n");
    }
}