qsort library 사용법 본문

Programming/C

qsort library 사용법

halatha 2008. 7. 2. 15:17
gcc 3.4.5 (mingw), Windows XP

1. integer 정렬

#include <stdio.h>

#include <stdlib.h>

int intcompare(const void* p1, const void* p2)

{

    int    i = *((int *)p1);

    int    j = *((int *)p2);

    if ( i > j )

        return (1);

    if ( i < j )

        return (-1);

    return (0);

}

int main()

{

    int    i;

    int    a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

    size_t    nelems = sizeof(a) / sizeof(int);

    qsort((void *)a, nelems, sizeof(int), intcompare);

    for ( i = 0; i < nelems; ++i )

    {

        (void) printf("%d ", a[i]);

    }

    (void) printf("\n");

    return (0);

}

1-1. integer 정렬

/*
    마소 주니어    2004.02
    page    27
*/
#include <stdio.h>
#include <stdlib.h>

static int compareInt2(const void* p_vpLeft, const void* p_vpRight)
{
    int    z_iLeft        = *( (int *) p_vpLeft );
    int    z_iRight    = *( (int *) p_vpRight );

    if ( z_iLeft > z_iRight )
        return -1;

    if ( z_iLeft == z_iRight )
        return 0;

    return 1;
}

static int compareInt(const void* p_vpLeft, const void* p_vpRight)
{
    int    z_iLeft        = *( (int *) p_vpLeft );
    int    z_iRight    = *( (int *) p_vpRight );

    if ( z_iLeft > z_iRight )
        return 1;

    if ( z_iLeft == z_iRight )
        return 0;

    return -1;
}

int main()
{
    int    z_iLoop = 0;
    int    z_iArray[] = { 10, 3, 5, 2, 1, 6, 51 };

    qsort(z_iArray, 7, sizeof(int), compareInt2);
    for ( z_iLoop = 0; z_iLoop < 7; ++z_iLoop )
    {
        printf("array[%d] = %d\n", z_iLoop, z_iArray[z_iLoop]);
    }

    printf("\n");

    qsort(z_iArray, 7, sizeof(int), compareInt);
    for ( z_iLoop = 0; z_iLoop < 7; ++z_iLoop )
    {
        printf("array[%d] = %d\n", z_iLoop, z_iArray[z_iLoop]);
    }

    return 0;
}

2. structure 정렬

#include <stdio.h>

#include <stdlib.h>

typedef struct

{

    int    m_iValue;

    char    m_cArrName[10];

}    TestStr_t;

int strcompare(const void* p1, const void* p2)

{

    TestStr_t    i = *((TestStr_t *)p1);

    TestStr_t    j = *((TestStr_t *)p2);

    if ( i.m_iValue > j.m_iValue )

        return (1);

    if ( i.m_iValue < j.m_iValue )

        return (-1);

    return (0);

}

void shell_sort(void* base, size_t nelem, size_t width, int (*fcmp)(const void*, const void*))

{

    int i, j, k, h;

    void* v;

    v = malloc(width);

    for ( h = 1; h < nelem; h = 3 * h + 1);

    for ( h /= 3; h > 0; h /= 3 )

    {

        for ( i = 0; i < h; ++i )

        {

            for ( j = i + h; j < nelem; j += h )

            {

                memcpy(v, (char*)base + j * width, width);

                k = j;

                while ( k > h - 1 && fcmp((char*)base + (k - h) * width, v) > 0 )

                {

                    memcpy((char*)base + k * width, (char*)base + (k - h) * width, width);

                    k -= h;

                }

                memcpy((char*)base + k * width, v, width);

            }

        }

    }

    free(v);

}

int main()

{

    int    i;

    int    z_iStrNum = 5;

    size_t    nelems;

    TestStr_t*    z_stpTestStr;

    z_stpTestStr = (TestStr_t*)malloc(sizeof(TestStr_t) * z_iStrNum);

    memset(z_stpTestStr, '\0', sizeof(z_stpTestStr));

    z_stpTestStr->m_iValue = 5;

    strcpy(z_stpTestStr->m_cArrName, "hahaha");

    ++z_stpTestStr;

    memset(z_stpTestStr, '\0', sizeof(z_stpTestStr));

    z_stpTestStr->m_iValue = 4;

    strcpy(z_stpTestStr->m_cArrName, "org2nd");

    ++z_stpTestStr;

    memset(z_stpTestStr, '\0', sizeof(z_stpTestStr));

    z_stpTestStr->m_iValue = 3;

    strcpy(z_stpTestStr->m_cArrName, "org3rd");

    ++z_stpTestStr;

    memset(z_stpTestStr, '\0', sizeof(z_stpTestStr));

    z_stpTestStr->m_iValue = 2;

    strcpy(z_stpTestStr->m_cArrName, "org4th");

    ++z_stpTestStr;

    memset(z_stpTestStr, '\0', sizeof(z_stpTestStr));

    z_stpTestStr->m_iValue = 1;

//    memset(z_stpTestStr->m_cArrName, '\0', 10);

    strcpy(z_stpTestStr->m_cArrName, "org5th");

    ++z_stpTestStr;

    z_stpTestStr -= z_iStrNum;

    for ( i = 0; i < z_iStrNum; ++i )

    {

        (void) printf("%d\t%s\n", z_stpTestStr->m_iValue, z_stpTestStr->m_cArrName);

        ++z_stpTestStr;

    }

    (void) printf("\n");

    z_stpTestStr -= z_iStrNum;

    nelems = (sizeof(z_stpTestStr) * z_iStrNum) / sizeof(z_stpTestStr);

    qsort((void *)z_stpTestStr, nelems, sizeof(TestStr_t), strcompare);

    printf("sizeof(TestStr_t) : %d\n\n", sizeof(TestStr_t));

//    shell_sort((void *)z_stpTestStr, nelems, sizeof(TestStr_t), strcompare);

    for ( i = 0; i < nelems; ++i )

    {

        (void) printf("%d\t%s\n", z_stpTestStr->m_iValue, z_stpTestStr->m_cArrName);

        ++z_stpTestStr;

    }

    (void) printf("\n");

    free(z_stpTestStr);

    return (0);

}


3. string 정렬

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/* 정렬할 critter들의 배열을 정의하라. */

struct critter

{

    const char    *name;

    const char    *species;

};

struct critter muppets[] =

{

    {"Kermit", "frog"},

    {"Piggy", "pig"},

    {"Gonzo", "whatever"},

    {"Fozzie", "bear"},

    {"Sam", "eagle"},

    {"Robin", "frog"},

    {"Animal", "animal"},

    {"Camilla", "chicken"},

    {"Sweetums", "monster"},

    {"Dr. Strangepork", "pig"},

    {"Link Hogthrob", "pig"},

    {"Zoot", "human"},

    {"Dr. Bunsen Honeydew", "human"},

    {"Beaker", "human"},

    {"Swedish Chef", "human"}

};

int count = sizeof (muppets) / sizeof (struct critter);

/* 이것은 정렬과 탐색을 위해 사용하는 비교함수이다. */

int

critter_cmp (const struct critter *c1, const struct critter *c2)

{

    return strcmp (c1->name, c2->name);

}

/* critter에 대한 정보를 프린트하라. */

void

print_critter (const struct critter *c)

{

    printf ("%s, the %s\n", c->name, c->species);

}

/* 정렬된 배열을 살펴보라 */

void

find_critter (const char *name)

{

    struct critter target, *result;

    target. name = name;

    result = bsearch(&target, muppets, count, sizeof (struct critter), critter_cmp);

    if (result)

    print_critter (result);

    else

    printf ("Couldn't find %s. \n", name);

}

/* Main 함수 */

int

main (void)

{

    int i;

    

    for (i = 0; i < count; i++)

    print_critter (&muppets[i]);

    printf ("\n");

    

    qsort ((void *)muppets, count, sizeof (struct critter), critter_cmp);

    

    for (i = 0; i < count; i++)

    print_critter (&muppets[i]);

    printf ("\n");

    

    find_critter ("Kermit");

    find_critter ("Gonzo");

    find_critter ("Janice");

    

    return 0;

}

Comments