Results 1 to 5 of 5

Thread: Use of qsort() : C

  1. #1
    Join Date
    Dec 2009
    Posts
    25

    Use of qsort() : C

    Hello, I am the student of the student of the Bsc. I had just started to learn the C language. I do not have the knowledge of the C language. I have to create the project on the qsort() function in C language. So, I want to know about the qsort() function. I also want to know how can I Use the qsort() function in C language. Thus, if anyone has the answer for me then reply me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Use of qsort() : C

    Hi, The qsort() function can be used for an Array to sort them. The qsort() function can use the Sedgewick's Quicksort algorithm to sort an array. Every element in the array can be the width bytes in size. the following can be the syntax of the qsort() function :
    Code:
    #include <stdlib.h>
    void qsort( void *base, size_t num, 
    size_t width, int (*compar) ( const void *, const void *) );

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Program : Use of qsort()

    The Following program can demonstrates you the working of the qsort() function :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char *ChrVt[] = { "last", "middle", "first" };
    int compare( const void *op1, const void *op2 )
      {
        const char **p3 = (const char **) op3;
        const char **p4 = (const char **) op4;
        return( strcmp( *p3, *p4 ) );
      }
    int main( void )
      {
        qsort( ChrVt, sizeof(ChrVt)/sizeof(char *),
          sizeof(char *), compare );
        printf( "%s %s %s\n",
            ChrVt[0], ChrVt[1], ChrVt[2] );
        return( EXIT_SUCCESS );
      }
    The Output of the above :
    Code:
    first last middle

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    qsort() : C

    Program :
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int compare_doubles (const void *Y, const void *Z)
    {
           double y = *((double *)Y);
           double z = *((double *)Z);
           if (y > z)
           {
                   return 1;
           }
           else
           {
                   if (y < z)
                   {
                           return -1;
                   }
                   else
                   {
                           return 0;
                   }
           }
    }
    int main ()
    {
           int j = 0, nRW;
           printf("Enter #ROWs: ");
           scanf("%d", &nROW);
           FILE *iPr = fopen("in.txt", "r");
           double *b;
           b = malloc(sizeof(double) * nRW);
           while (!feof(iPr))
           {
                   fscanf(iPr, "%lf,", &b[j]);
                   printf("%lf\n", b[j]);
                   j++;
           }
           qsort((void *)b, nRW, sizeof(double), compare_doubles);
           printf("after sorting:\n");
           for(j = 0; j < nRW; j++)
           {
                   printf("%f\n", b[j]);
           }
           return 0;
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Use of qsort() : C

    Hi, The qsort() function can be used for the to sort the buf num items, each of using Quicksort. To compare the buffer items compare function can be use.
    The compare function can returns the following :
    1. zero if they are equal
    2. positive if the first argument is greater than the second.
    3. compare should return negative if the first argument is less than the second
    The qsort() function can sorts the given buffer in ascending order.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,748,249.27572 seconds with 15 queries