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 622013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:56:17+00:00 2026-05-13T18:56:17+00:00

If I have two functions: void SortStudents(char *studentList[], size_t studentCount) { qsort(studentList, sizeof(studentList)/sizeof(studentList[0]), sizeof(studentList[0]),

  • 0

If I have two functions:

void SortStudents(char *studentList[], size_t studentCount) 
{
    qsort(studentList, sizeof(studentList)/sizeof(studentList[0]), sizeof(studentList[0]), Compare);
}

int Compare(const void *a, const void *b) 
{
    return (strcmp(*(char **)a, *(char **)b));
}

That sort and compare using the qsort function, how do I use bsearch to find subsets of my list. For example, if I have two lists:

  • (List A) Bob, Jimmy, Lee, James, Anne
  • (List B) Jen, Jon, Lee, James, Steph

How do I search in List B to find those elements in A?

Can you also do a search in List B to find those elements not in A?

Thanks.

  • 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-05-13T18:56:18+00:00Added an answer on May 13, 2026 at 6:56 pm

    To do a search, you have to use a one-item list as the key parameter to ‘bsearch()’.

    In context, searching for the entry at a_list[n] in b_list:

     void *found = bsearch(&a_list[n], b_list, b_list, b_size, Compare);
    

    So, to find the elements in List B that are in List A, you will do:

    • Sort List B (you do not need to sort List A for this part of the exercise unless you want to)
    • For each element in List A, search for the item in (the sorted) List B.

    And to find the elements in B that are not in A, you will need to sort List A after all and then for each element in List B, see whether the element is in List A using the reversed search.


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char *a_list[] = { "Bob", "Jimmy", "Lee", "James", "Anne"  };
    static char *b_list[] = { "Jen", "Jon",   "Lee", "James", "Steph" };
    static size_t a_number = sizeof(a_list)/sizeof(a_list[0]);
    static size_t b_number = sizeof(b_list)/sizeof(b_list[0]);
    
    static int Compare(const void *a, const void *b) 
    {
        return (strcmp(*(char **)a, *(char **)b));
    }
    
    void SortStudents(char *studentList[], size_t studentCount) 
    {
        qsort(studentList, studentCount, sizeof(studentList[0]), Compare);
    }
    
    static void dump_list(const char *tag, char **list, size_t number)
    {
        size_t i;
        printf("%s:\n", tag);
        for (i = 0; i < number; i++)
            printf(" %s%s", list[i], (i == number - 1) ? "" : ",");
        putchar('\n');
    }
    
    static char *search_list(char *name, char **list, size_t number)
    {
        char **found = bsearch(&name, list, number, sizeof(*list), Compare);
        return((found == 0) ? 0 : *found);
    }
    
    static void names_in_list(char **find_list, size_t find_number, char **name_list, size_t name_number)
    {
        size_t i;
        for (i = 0; i < find_number; i++)
        {
            char *name = search_list(find_list[i], name_list, name_number);
            if (name != 0)
                printf("Found %s in list at %s\n", find_list[i], name);
        }
    }
    
    static void names_not_in_list(char **find_list, size_t find_number, char **name_list, size_t name_number)
    {
        size_t i;
        for (i = 0; i < find_number; i++)
        {
            char *name = search_list(find_list[i], name_list, name_number);
            if (name == 0)
                printf("Did not find %s in list\n", find_list[i]);
        }
    }
    
    int main(void)
    {
        dump_list("Unsorted A list", a_list, a_number);
        dump_list("Unsorted B list", b_list, b_number);
        SortStudents(a_list, a_number);
        SortStudents(b_list, b_number);
        dump_list("Sorted A list", a_list, a_number);
        dump_list("Sorted B list", b_list, b_number);
        dump_list("Searching in B list for people in A list", b_list, b_number);
        names_in_list(a_list, a_number, b_list, b_number);
        dump_list("Searching in A list for people not in B list", a_list, a_number);
        names_not_in_list(b_list, b_number, a_list, a_number);
        return(0);
    }
    

    And the output was:

    Unsorted A list:
     Bob, Jimmy, Lee, James, Anne
    Unsorted B list:
     Jen, Jon, Lee, James, Steph
    Sorted A list:
     Anne, Bob, James, Jimmy, Lee
    Sorted B list:
     James, Jen, Jon, Lee, Steph
    Searching in B list for people in A list:
     James, Jen, Jon, Lee, Steph
    Found James in list at James
    Found Lee in list at Lee
    Searching in A list for people not in B list:
     Anne, Bob, James, Jimmy, Lee
    Did not find Jen in list
    Did not find Jon in list
    Did not find Steph in list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions : void foo(const char * p) and template<size_t T_Size> void
Suppose I have two functions which look like this: public static void myFunction1(int a,
Suppose I have two C++ functions for debug output: void Trace( const wchar_t* format,
Assume that I have these two overloaded functions. public static void Main(string[]args) { int
I have two functions in an ActionScript class, they are: private function loaderCompleteHandler(event:Event):void {
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
I have two functions in C: void function1(){ // do something } void function2(){
I have two functions: void free_this(THIS *this) { THIS *this_tmp; while (this_tmp = this)
I have the following two functions: public class MyClass { public void Save<TObject>(TObject object)
I have two functions: void prepare() and void finish() that will be called sequentially

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.