Thread: QSort on structure array

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    53

    QSort on structure array

    I have a structure array that is created in a function locally, in order to copy to it the contents of a linked list so that they can be sorted and then printed.

    How do I implement qsort in this case?

    Code:
    /* Initialize record structure for each soldier */
    struct record {
    	long int aa;
    	char firstname[20];
    	char lastname[30];
    	struct sdate datein;
    	struct sdate dateout;
    	int freedays;
    	int servicedays;
    } soldiers;
    
    /* Initialize nodes for linked list */
    struct node {
    	struct record	soldier;
    	struct node*	next;
    };
    And then in the function that is called when we want to sort the linked list basied on servicedays...

    Code:
    void servicequeue(struct node* head, int number, int countrec)
    {
    	int i;
    	struct record queue[countrec];
    	struct node* current = head;
    	
    	for (i=0;i<countrec;i++) {
    		queue[i] = current->soldier;
    		current = current->next;
    	}
    	
    	qsort((void *) queue, countrec, sizeof (struct record), compare(queue));
    	
    	printf ("Yphresia prepei na kanoun oi parakatw:\n");
    	for (i=0;i<number;i++) {
    		printf ("&#37;s %s\n", queue[i].firstname, queue[i].lastname);
    	}
    }
    
    int compare(struct record queue, const void *p1, const void *p2)
    {
    	return (((queue*)p1)->servicedays - ((queue*)p2)->servicedays);
    }
    ...which obviously doesn't work, and it was a long shot based on a suggestion in another thread. Anyone able to help me get it right?

    The example I saw didn't have any struct element in the compare function because the structure was globally declared. What should I do now that it's declared locally? I only need one tiny correction.

    countrec is the number of records in the linkedlist, number is how many elements we want to show from the top after we sort.
    Last edited by Leftos; 01-13-2008 at 06:54 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing the signature of the compare function appears to be wrong.

    qsort reference
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    53
    Exactly, that's my problem. I need to know how to propery define, declare and call compare, since the struct is declared locally and not globally, which is what the example I read assumed.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    From what I see the struct is declared globally (the array is local). The name of your struct should do just as good as int in the example.

    That is, the compare function is supposed to compare any two instances of this struct, not any two elements in a particular array.

    If you still have trouble try creating a minimal compilable example (that can produce your errors), by removing stuff like sdate and anything else that is not essential to the problem.
    Last edited by anon; 01-13-2008 at 09:08 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In other words, your comparator function needs to only take two pointers, and return an int. In the invocation of qsort, you should just pass the function pointer == the name of the function by itself with no parentheses.

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    By the way, it would be a bit more efficient to sort using pointer on struct record instead of struct record, since your struct is rather large (i can't know exactly but i guess it may be something like 80 bytes vs ~4 bytes for a pointer). Of course, if your data set is small, it doesn't really matters...

    It could looks like...

    Code:
    int compare(const void *p1, const void *p2)
    {
    	return (* (struct record **) p1)->servicedays - (* (struct record **) p2)->servicedays;
    }
    
    void servicequeue(struct node* head, int number, int countrec)
    {
    	int i;
    	struct record* queue[countrec];
    	
    	for (i = 0; i < countrec; i++) {
    		queue[i] = &(current->soldier);
    		current = current->next;
    	}
    	
    	qsort(queue, countrec, sizeof(struct record *), compare);
    	
    	printf ("Yphresia prepei na kanoun oi parakatw:\n");
    	for (i=0;i<number;i++) {
    		printf ("%s %s\n", queue[i]->firstname, queue[i]->lastname);
    	}
    }
    This does look a little bit ankward. Didn't test, maybe there's errors. In fact, i didn't even know the qsort function was part of the standard C library.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    53
    Well, the recommended way to sort it was "bubblesort" because that's the "simplest" way to do it. I wouldn't learn anything new if I bubblesorted it though, would I? That's why I'm trying to get it to work. Thank you all. I'll try your recommendations soon and get back with the results.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Depends on how you implement the bubble-sort. Does it takes a compare function that takes two void pointers and returns an int, so you can quickly change what can be sorted?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    53
    foxman, your corrections worked perfectly, many thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM