簡體   English   中英

數組中的字符串順序

[英]Strings order in Array

我有下一個代碼,
你猜我希望看到 output 最后一行: a, b,c,d但實際的 output 是:

a d (null) (null)  
a c d (null)  
a b c d  
a b b c

最后一行,為什么我看到它?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
    char *str_tmp[4] = {"d", "a", "c", "b"};
    char **str = calloc(4, sizeof(char *));

    str[0] = str_tmp[0];

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (strcmp(str_tmp[i], str[j]) < 0)
            {
                for (int k = i; k > j; k--)
                {
                    str[k] = str[k-1];
                }

                str[j] = str_tmp[i];

                for (int n = 0; n < 4; n++)
                {
                    printf("%s ", str[n]);
                }
                printf("\n");
            }
        }
    }

    return 0;
}

原因是這一行str[j] = str_tmp[i]; 仍然存在的最少元素是b ,您需要交換它

請使用簡單的方法進行交換

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
    char *str_tmp[4] = {"d", "a", "c", "b"};
    char *temp = "";

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (strcmp(str_tmp[i], str_tmp[j]) < 0)
            {
                temp = str_tmp[i];
                str_tmp[i] = str_tmp[j];
                str_tmp[j] = temp;
            }
        }
    }

    for (int n = 0; n < 4; n++)
    {
         printf("%s ", str_tmp[n]);
    }

    return 0;
}

問題是這個循環

    for (int j = 0; j < i; j++)
    {
        if (strcmp(str_tmp[i], str[j]) < 0)
        {
            for (int k = i; k > j; k--)
            {
                str[k] = str[k-1];
            }

            str[j] = str_tmp[i];

            for (int n = 0; n < 4; n++)
            {
                printf("%s ", str[n]);
            }
            printf("\n");
        }
    }

當 if 語句中的條件出現時不會停止其迭代

        if (strcmp(str_tmp[i], str[j]) < 0)

很滿意。

因此,當您在目標數組中有以下值時

a c d (null)

那么對於 j 等於 1 的條件

strcmp(str_tmp[i], str[j]) < 0

評估為真,結果你得到

a b c d

但是循環繼續它的迭代並且對於 j 再次等於 2 條件

strcmp(str_tmp[i], str[j]) < 0

產生真,結果元素 str_tmp[i[ 插入 position 2 ,你得到

a b b c

一旦滿足條件,您必須立即中斷循環。

這是您更新的程序。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) 
{
    char *str_tmp[4] = {"d", "a", "c", "b"};
    char **str = calloc(4, sizeof(char *));

    str[0] = str_tmp[0];

    for (int i = 0; i < 4; i++)
    {
        int j = 0;
        while ( j < i && !( strcmp(str_tmp[i], str[j]) < 0 ) ) j++;
        for (int k = i; k > j; k--)
        {
             str[k] = str[k-1];
        }

        str[j] = str_tmp[i];

        for (int n = 0; n < i + 1; n++)
        {
             printf("%s ", str[n]);
        }
        printf("\n");
    }

    free( str );

    return 0;
}

它的 output 是

d 
a d 
a c d 
a b c d 

事實上,一個循環是多余的。 使用像 4 這樣的幻數也是一個壞主意。程序可以用以下方式重寫

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) 
{
    char *str_tmp[] = { "d", "a", "c", "b" };
    const size_t N = sizeof( str_tmp ) / sizeof( *str_tmp );
    char **str = calloc( N, sizeof( char * ) );

    for ( size_t i = 0; i < N; i++ )
    {
        size_t j = i;

        for ( ; j != 0 && strcmp( str_tmp[i], str[j-1] ) < 0; j-- )
        {
             str[j] = str[j-1];
        }

        str[j] = str_tmp[i];

        for ( size_t n = 0; n < i + 1; n++ )
        {
             printf( "%s ", str[n] );
        }

        putchar( '\n' );
    }

    free( str );

    return 0;
}

由於現在程序不依賴於幻數 4,您可以向數組tmp_str添加任意數量的新元素,並且程序無需任何其他更改即可工作。 例如,如果您通過以下方式使用初始化器填充數組 tmp_str

 char *str_tmp[] = { "d", "a", "c", "b", "k", "j", "z", "t" };

那么程序 output 將是

d 
a d 
a c d 
a b c d 
a b c d k 
a b c d j k 
a b c d j k z 
a b c d j k t z 

您可以將插入排序方法的代碼移動到單獨的 function 中。 例如

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void insertion_sort( char * s[], size_t n )
{
    for ( size_t i = 1; i < n; i++ )
    {
        if ( strcmp( s[i], s[i-1] )  < 0 )
        {
            char *item = s[i];

            size_t j = i;

            do
            {
                s[j] = s[j-1];
            } while ( --j != 0 && strcmp( item, s[j-1] ) < 0 );

            s[j] = item;
        }           
    }
}

int main(void) 
{
    char *s[] = { "d", "a", "c", "b", "k", "j", "z", "t" };
    const size_t N = sizeof( s ) / sizeof( *s );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%s ", s[i] );
    }

    putchar( '\n' );

    insertion_sort( s, N );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%s ", s[i] );
    }

    putchar( '\n' );

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM