Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15564

Re: Is there any way to refer to another array in qsort's compare function?

Message-ID <4F08F66B.4899@mindspring.com> (permalink)
Date 2012-01-07 20:50 -0500
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: Is there any way to refer to another array in qsort's compare function?
References <f7d05964-b7de-45b1-afde-7ac9d7c38e9b@x34g2000prb.googlegroups.com>

Show all headers | View raw


Stone wrote:
> 
> Here's the problem.
> 
> I wrote a function 'int my_compare(const void *, const void *);' and
> passed it to qsort() of stdlib.h, in order to sort an array named
> *ptr* which contains indices of another array *data*. By that I
> intended to sort elements of *ptr* (i.e. ptr[i]) using data[ptr[i]] as
> the key value.
> 
> However, the *data* array was defined as static in main(), and seemed
> not easy to access in my_compare(). Is there any way to achieve this?

If you have for parameters in a function: 
     a pointer to the first element of data[]
     the number of elements in data[]
     the size of the elements in data[]
then 
you can derive an array of pointers to the elements of data[] 
and you can sort them using qsort
and you can derive a sorted array of indexes 
    from the sorted array of pointers.

/* BEGIN output from index_sort_test.c */

Array elements:    Indexed by string length: Printed by string length:

data[0] is zero    ptr[0] is 1               data[ptr[0]] is one
data[1] is one     ptr[1] is 2               data[ptr[1]] is two
data[2] is two     ptr[2] is 6               data[ptr[2]] is six
data[3] is three   ptr[3] is 5               data[ptr[3]] is five
data[4] is four    ptr[4] is 4               data[ptr[4]] is four
data[5] is five    ptr[5] is 0               data[ptr[5]] is zero
data[6] is six     ptr[6] is 9               data[ptr[6]] is nine
data[7] is seven   ptr[7] is 8               data[ptr[7]] is eight
data[8] is eight   ptr[8] is 7               data[ptr[8]] is seven
data[9] is nine    ptr[9] is 3               data[ptr[9]] is three

/* END output from index_sort_test.c */




/* BEGIN index_sort_test.c */

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

#define NMEMB(A)    (sizeof (A) /  sizeof *(A))

int 
my_compare(const void *a, const void *b)
{
    const char (**const aa)[sizeof "three"] = a;
    const char (**const bb)[sizeof "three"] = b;
    const size_t len_a = strlen(**aa);
    const size_t len_b = strlen(**bb);

    return len_b > len_a ? -1 : len_b != len_a;
}

void
index_sort(size_t *index, 
           char **pointer, 
           void *base, 
           size_t nmemb,
           size_t size, 
           int (*compar)(const void *, const void *))
{
    size_t n;
    char *const array = base;
    
    pointer[0] = array;
    for (n = 1; nmemb > n; ++n) {
        pointer[n] = pointer[n - 1] + size;
    }
    qsort(pointer, nmemb, sizeof *pointer, compar);
    for (n = 0; n != nmemb; ++n) {
        index[n] = (pointer[n] - array) / size;
    }
}

int 
main(void)
{
    unsigned n;
    static char data[][sizeof "three"] = {
        "zero", "one",   "two", "three", "four",
        "five", "six", "seven", "eight", "nine"
    };
    size_t ptr[NMEMB(data)]; 
    char *pointer[NMEMB(data)];

    index_sort(
        ptr,
        pointer,
        data, 
        NMEMB(data),  
        sizeof *data, 
        my_compare
        );
    puts("\n/* BEGIN output from index_sort_test.c */\n");
    puts("Array elements:    Indexed by string length: "
         "Printed by string length:\n");
    for (n = 0; n != NMEMB(data); ++n) {
        printf("data[%u] is %-7s ",     n,          data[n]);
        printf("ptr[%u] is %-15u ",     n, (unsigned)ptr[n]);     
        printf("data[ptr[%u]] is %s\n", n,      data[ptr[n]]);
    }
    puts("\n/* END output from index_sort_test.c */");
    return 0;
}

/* END index_sort_test.c */

-- 
pete

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Is there any way to refer to another array in qsort's compare function? Stone <stonekyx@gmail.com> - 2011-12-29 00:27 -0800
  Re: Is there any way to refer to another array in qsort's compare function? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-29 08:57 +0000
  Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 11:58 +0000
    Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 04:20 -0800
      Re: Is there any way to refer to another array in qsort's compare function? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-29 12:37 +0000
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 04:48 -0800
          Re: Is there any way to refer to another array in qsort's compare function? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-29 12:59 +0000
            Re: Is there any way to refer to another array in qsort's compare function? Geoff <geoff@invalid.invalid> - 2011-12-29 10:09 -0800
              Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 17:50 -0800
                Re: Is there any way to refer to another array in qsort's compare function? Geoff <geoff@invalid.invalid> - 2011-12-29 19:29 -0800
                Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-30 00:36 -0800
                Re: Is there any way to refer to another array in qsort's compare function? Bull in a China Blue Shop <chine.bleu@yahoo.com> - 2011-12-30 00:52 -0800
                Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-30 02:16 -0800
              Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 18:01 -0800
            Re: Is there any way to refer to another array in qsort's compare function? James Kuyper <jameskuyper@verizon.net> - 2011-12-29 13:21 -0500
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-30 23:18 -0800
    Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-07 23:40 -0800
      Re: Is there any way to refer to another array in qsort's compare function? pete <pfiland@mindspring.com> - 2012-01-08 07:26 -0500
        Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-08 12:50 +0000
          Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-08 06:02 -0800
        Re: Is there any way to refer to another array in qsort's compare function? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-01-17 12:35 +0200
  Re: Is there any way to refer to another array in qsort's compare function? Bull in a China Blue Shop <chine.bleu@yahoo.com> - 2011-12-29 05:07 -0800
    Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 14:16 +0000
      Re: Is there any way to refer to another array in qsort's compare function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-29 15:29 +0000
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 09:33 -0800
        Re: Is there any way to refer to another array in qsort's compare function? 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-29 15:40 -0800
  Re: Is there any way to refer to another array in qsort's compare function? James Kuyper <jameskuyper@verizon.net> - 2011-12-29 10:50 -0500
  Re: Is there any way to refer to another array in qsort's compare function? pete <pfiland@mindspring.com> - 2012-01-07 20:50 -0500

csiph-web