Sorting array of pointers to int 
Author Message
 Sorting array of pointers to int

I have a linked list of structures, of which one member is of type int. I have
also created an array of pointers to each int member and have tried in vain to
figure out how to use quicksort to sort this array so that I can then search
on the sorted array (binary search) and pull out any match(es) from the linked
list of structures.

I can sort an array of ints without difficulty, but sorting the array of
pointers to int has eluded me. Any suggestions?

 -- Robert




Wed, 26 Sep 2001 03:00:00 GMT  
 Sorting array of pointers to int
: I have a linked list of structures, of which one member is of type int. I have
: also created an array of pointers to each int member and have tried in vain to
: figure out how to use quicksort to sort this array so that I can then search
: on the sorted array (binary search) and pull out any match(es) from the linked
: list of structures.

: I can sort an array of ints without difficulty, but sorting the array of
: pointers to int has eluded me. Any suggestions?

The only change is in the comparison function, where you have to cast
back the arguments to type pointer to pointer to int, dereference to
get the related integers, and return a comparison of the results.
(I've assumed you're using the standard qsort() here; if it's your
own implementation, then rewrite the comparison function accordingly).

Will



Wed, 26 Sep 2001 03:00:00 GMT  
 Sorting array of pointers to int

Quote:
>I have a linked list of structures, of which one member is of type int. I have
>also created an array of pointers to each int member and have tried in vain to
>figure out how to use quicksort to sort this array so that I can then search
>on the sorted array (binary search) and pull out any match(es) from the linked
>list of structures.

>I can sort an array of ints without difficulty, but sorting the array of
>pointers to int has eluded me. Any suggestions?

When you're performing the swap operations, be sure to swap the pointers
to int, not the int values themselves. You must use the int values
themselves for the comparison operation, not the pointers!

--
{ Sunil Rao }
"Is there any light more becoming than that, low and richly tinted, that comes
subdued through the mists of sunshine?"
        -- J Sheridan Le Fanu



Wed, 26 Sep 2001 03:00:00 GMT  
 Sorting array of pointers to int
Thanks to those who responded. I've got it working now and all is right with
the world!

 -- Robert




Fri, 28 Sep 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. int pointer to int array

2. problem with sorting int array

3. sorting an int array

4. Sorting an Array of pointers to structures

5. pointers, arrays and sorting

6. sorting an array using pointers

7. sorting , pointers and arrays

8. Sorting an array using pointers

9. how to modify a pointer to an array one sizeof(int)

10. pointer to int array question

11. passing of pointers to int arrays?

12. Returning Int array pointers from functions

 

 
Powered by phpBB® Forum Software