Sort array of structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sevak316
    New Member
    • Sep 2008
    • 73

    Sort array of structures

    I have an array of structures that I would like to sort.

    typedef struct
    {
    int param;
    int priority;
    } mystruct;

    I have an array of them.

    mystruct myarray[10];

    How can I quick sort them. I have been googling around and found couple of examples, but wanted to see if you guys have a shorter method.

    I want to sort the array by the structures priority.


    Thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    This example tells you all you need.

    kind regards,

    Jos

    Comment

    • sevak316
      New Member
      • Sep 2008
      • 73

      #3
      Thank you very much for the example, it really helped. Here is what I have. It works, but I really want to understand what it does. What is the return statement in "compfunc_i nt" doing? Also, how is it getting its parameters??

      Thanks.

      Code:
      typedef struct {
      int priority;
      int id;
      } my_struct;
      
      my_struct my_array[8];
      
      void quickSort(void)
      {
      int i;
      printf("--------------------Unsorted------------------\n\n");
      for(i = 0; i < 8; i++)
         printf("%d\n", OsThreads[i].priority);
      
      qsort(my_array, 8, sizeof(my_struct), compfunc_int);
      
      printf("---------------------Sorted------------------\n\n");
      for(i = 0; i < 8; i++)
         printf("%d\n", OsThreads[i].priority);
      
      }
      
      
      int compfunc_int(const void *x, const void *y)
      {
        //WHAT DOES THIS LINE DO??
       
        return ((my_struct *)x)->priority -((my_struct *)y)->priority;
      }

      Comment

      • curiously enough
        New Member
        • Aug 2008
        • 79

        #4
        Code:
        Code removed per posting guidelines
               }

        Comment

        • sevak316
          New Member
          • Sep 2008
          • 73

          #5
          thanks curiously enough.

          i like my code to be simple!

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by sevak316
            thanks curiously enough.

            i like my code to be simple!
            Then better stick to that qsort() method because a bubble sort method is one of
            the worst sorting methods in existence.

            kind regards,

            Jos

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              The Standard Library provides a generic sort function (qsort). You should consider using it unless the point of your exercise is to demonstrate that you can implement a sort algorithm. To use qsort you need to write your own comparison function.

              Comment

              • Tassos Souris
                New Member
                • Aug 2008
                • 152

                #8
                Code:
                int compfunc_int(const void *x, const void *y){
                
                   return ((my_struct *)x)->priority -((my_struct *)y)->priority;
                
                }

                See this equivalent:

                Code:
                int comparison( const void *_x, const void *_y ){
                
                   my_struct *x = _x;  
                   my_struct *y = _y;
                
                   assert( x != NULL );
                   assert( y != NULL );
                
                
                   return ( x->priority - y->priority );
                
                }
                I think it is obvious now...

                Comment

                Working...