Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Shell排序函数未对数组进行完全排序_C_Arrays_Algorithm_Sorting_Debugging - Fatal编程技术网

C Shell排序函数未对数组进行完全排序

C Shell排序函数未对数组进行完全排序,c,arrays,algorithm,sorting,debugging,C,Arrays,Algorithm,Sorting,Debugging,我有一个由10个整数组成的数组,我想对其进行排序,但最终的数组似乎没有完全排序 // Shell sort Function void shell_sort(int A[]){ display_array(A); int k = ARRAY_SIZE / 2; int x = 0; // index of value that swaps with value k spaces back int temp = 0; while (k > 0){

我有一个由10个整数组成的数组,我想对其进行排序,但最终的数组似乎没有完全排序

// Shell sort Function

void shell_sort(int A[]){
    display_array(A);

    int k = ARRAY_SIZE / 2;
    int x = 0; // index of value that swaps with value k spaces back
    int temp = 0;

    while (k > 0){

        for (int i = ARRAY_SIZE-1; i >= k; i-- ){
            x = i;
            while( (x - k) >= 0 ){

                if ( A[x] < A[x-k] ){
                    temp = A[x];
                    A[x] = A[x-k];
                    A[x-k] = temp;
                    x -= k ;
                }
                else{
                    break;
                }
            }
        }
        printf("k=%d\n",k);
        display_array(A);
        k /= 2;
    }
}

你能指出我错在哪里吗

我将k/=2更改为k--,得到了答案,但需要更多的循环。我不确定我应该这样做

以下是输出:

15  4   6   2   7   8   44  1   9   3   
k = 5
8   4   1   2   3   15  44  6   9   7   
k = 4
3   4   1   2   8   7   44  6   9   15  
k = 3
2   4   1   3   6   7   15  8   9   44  
k = 2
1   3   2   4   6   7   9   8   15  44  
k = 1
1   2   3   4   6   7   8   9   15  44

我对您的程序进行了如下重构:

#include <stdio.h>
#define ARRAY_SIZE 10
// Shell sort Function

void display_array(int A[]) {
    int i;
    for (i =0; i < ARRAY_SIZE; ++i)
        printf("%d ", A[i]);
    printf ("\n");
}

void shell_sort(int A[]){
    display_array(A);

    int temp = 0;
    int i,j,k;
    for (i = ARRAY_SIZE/2; i > 0; i = i/2 ){
        for (j = i; j < ARRAY_SIZE; ++j) {
            for (k = j -i; k >= 0; k = k - i) {
                if(A[k+i]>=A[k])
                    break;
                else
                    {
                        temp=A[k];
                        A[k]=A[k+i];
                        A[k+i]=temp;
                    }
                printf("k=%d\n",k);
                display_array(A);
            }
        }
    }
}

int main(void) {
    // your code goes here
    int A[ARRAY_SIZE] = {15,4,6,2,7,8,44,1,9,3};
    shell_sort(A);
    return 0;
}
请注意问题中提到的评论。您应该将数组大小传递给函数。然而,这是一个粗糙的实现

编辑

进一步重构的代码如下所示:

void shell_sort(int A[]){
    display_array(A);

    int k = ARRAY_SIZE/2; // This is the offset you want to begin with 
    int j = 0; // index of value that swaps with value k spaces back
    int temp = 0;
    int i;

    while (k > 0){
        // browse through the later half of the array k to ARRAY SIZE
        for (i = k; i < ARRAY_SIZE; i++ ) {
            j = i;
            int temp = A[i];
            // As long as j >= k, check if values can be swapped  
            while( (j - k) >= 0 && A[j-k] > temp ){
                A[j] = A[j-k];
                j = j -k;
            }
            // finally insert the correct value at j
            A[j] = temp;
        }
        printf("k=%d\n",k);
        display_array(A);
        k = k/2;
    }
}


希望这能有所帮助。

由于每个列表中只有两项,因此
k=5
步骤起作用。
k=2
步骤需要对
{8,1,3,44,9}
{4,2,15,6,7}
进行排序。但是
k=2
步骤之后的顺序是
{1,8,3,9,44}
{2,4,6,15,7}
。因此,代码无法正确处理长度为3或更大的数组。我的建议是使用<代码> {8,1,3,44,9} <代码>的输入数组设置<代码> k=1/代码>,并考虑代码为何不对该数组进行正确排序。K/= 2后面的推理是什么?难道你不应该经历所有的过失吗嗯。。我将k改为1,并使用{8,1,3,44,9}作为数组。我得到了这个答案[1 8 3 9 44]。我正在试图找出错误。@AmitKumar我刚刚在网上看到一个shell排序的视频,他们使用k=k/2,我试图实现它。一个接一个地缩小差距会更好吗?@user3386109同意,只要最后一个差距是1,它就应该适用于任何差距值。我想知道我的程序比这个实现有什么问题。此外,k(间隙)的值应尽可能低,以使程序有效。您所做的是直接向上插入排序,而不是shell排序。谢谢。我已经实现了shell排序,而不是插入排序。Shell排序本身是插入排序的一个通用版本。
15 4 6 2 7 8 44 1 9 3
k=0
8 4 6 2 7 15 44 1 9 3
k=2
8 4 1 2 7 15 44 6 9 3
k=4
8 4 1 2 3 15 44 6 9 7
k=0
1 4 8 2 3 15 44 6 9 7
k=1
1 2 8 4 3 15 44 6 9 7
k=2
1 2 3 4 8 15 44 6 9 7
k=5
1 2 3 4 8 6 44 15 9 7
k=6
1 2 3 4 8 6 9 15 44 7
k=7
1 2 3 4 8 6 9 7 44 15
k=4
1 2 3 4 6 8 9 7 44 15
k=6
1 2 3 4 6 8 7 9 44 15
k=5
1 2 3 4 6 7 8 9 44 15
k=8
1 2 3 4 6 7 8 9 15 44
void shell_sort(int A[]){
    display_array(A);

    int k = ARRAY_SIZE/2; // This is the offset you want to begin with 
    int j = 0; // index of value that swaps with value k spaces back
    int temp = 0;
    int i;

    while (k > 0){
        // browse through the later half of the array k to ARRAY SIZE
        for (i = k; i < ARRAY_SIZE; i++ ) {
            j = i;
            int temp = A[i];
            // As long as j >= k, check if values can be swapped  
            while( (j - k) >= 0 && A[j-k] > temp ){
                A[j] = A[j-k];
                j = j -k;
            }
            // finally insert the correct value at j
            A[j] = temp;
        }
        printf("k=%d\n",k);
        display_array(A);
        k = k/2;
    }
}
15 4 6 2 7 8 44 1 9 3
k=5
8 4 1 2 3 15 44 6 9 7
k=2
1 2 3 4 8 6 9 7 44 15
k=1
1 2 3 4 6 7 8 9 15 44