Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:50:17+00:00 2026-06-14T10:50:17+00:00

I am trying to find out how (using a quicksort algorithm) to sort an

  • 0

I am trying to find out how (using a quicksort algorithm) to sort an struct array by 2 criterias. For example say I had a struct of:

struct employee{
   char gender[12];
   char name[12];
   int id;
};

Say my input is:

struct employee arr[3]=
{
    {"male","Matt",1234},
    {"female","Jessica",2345},
    {"male","Josh",1235}
};

I am wanting to sort the elements by gender first then the IDs in ascending order. An example would be have all the males printed first with their IDs in order and then all the females with theirs. I am trying to do this without using the qsort function but I havent the slightest idea how to check . Here is my sorting function:

void quicksort(struct employee *arr, int left, int right)
{
    int pivot, l, r, temp;
    if(left < right)
    {
        p = left;
        l = left;
        r = right;
        while(l < r)
        {

            while(arr[l].id <= arr[p].id && l <= right)
                l++;
            while(arr[r].id > arr[p].id && r >= left)
                r--;

            if(l < r)
            {
                temp = arr[l].id;
                arr[l].id = arr[r].id;
                arr[r].id = temp;
            }
        }


        temp = arr[r].id;
        arr[r].id = arr[p].id;
        arr[p].id = temp;

        quicksort(arr, left, r-1);
        quicksort(arr, r+1, right);
    }
}

Any suggestions? I was thinking I could use strcmp but I cant figure out where to include it within the function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:50:18+00:00Added an answer on June 14, 2026 at 10:50 am

    You can certainly inline the comparison function, and a swapper for that matter. This code below is pretty basic and relies on valid pointers, but you’l get the idea. I also took the liberty of trimming down your quicksort, fixing what was off along the way (I hope).

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // employee record
    struct employee
    {
        char gender[12];
        char name[12];
        int id;
    };
    
    // swap employee records
    void swap_employee(struct employee *left, struct employee *right)
    {
        struct employee tmp = *right;
        *right = *left;
        *left = tmp;
    }
    
    // compare employee records
    int compare_employee(const struct employee* left,
                         const struct employee* right)
    {
        int gender = strcmp(left->gender, right->gender);
        return (gender ? gender : (left->id - right->id));
    }
    
    // quicksort for employees
    static void quicksort_(struct employee *arr, int left, int right)
    {
        struct employee p = arr[(left+right)/2];    // as good as any
        int l = left, r = right;   // movable indicies
    
        while (l <= r)
        {
            while (compare_employee(arr+l, &p) < 0)
                ++l;
            while (compare_employee(arr+r, &p) > 0)
                --r;
            if (l <= r)
            {
                swap_employee(arr+l, arr+r);
                ++l; --r;
            }
        }
    
        if (left < r)
            quicksort_(arr, left, r);
        if (l < right)
            quicksort_(arr, l, right);
    }
    
    // exposed API
    void quicksort(struct employee *arr, int count)
    {
        if (arr && (count>0))
            quicksort_(arr, 0, count-1);
    }
    
    /* sample usage */
    int main(int argc, char *argv[])
    {
        struct employee arr[]=
        {
            {"male","Matt",1234},
            {"female","Jessica",2345},
            {"male","Josh",1235},
            {"female","Betsy",2344},
            {"male","Roger",1233}
        };
    
        quicksort(arr, sizeof(arr)/sizeof(arr[0]));
    
        for (int i=0;i<sizeof(arr)/sizeof(arr[0]);++i)
            printf("%s, %s, %d\n", arr[i].gender,arr[i].name, arr[i].id);
    
        return EXIT_SUCCESS;
    }
    

    Results

    female, Betsy, 2344
    female, Jessica, 2345
    male, Roger, 1233
    male, Matt, 1234
    male, Josh, 1235
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out how to programmatically (I'm using C#) determine the name
I'm trying to find out the difference between using one colon : and two
using jQuery i am trying to find out all the URLS that user has
I'm trying to find out the system architecture using Java and I've tried out:
I'm trying to find out the 'best' option for using levenshtein() in PHP .
I'm using dotTrace Memory to trying to find out why an application leaks memory.
I'm trying to find out/understand exactly how you access data of an object using
I'm trying to find out if a row exists in a table. Using MySQL,
I'm trying to find out how to programmatically (i.e. without using the FieldAttribute )
I'm trying to find out who the friends of 'x' are, and using that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.