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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:35:12+00:00 2026-05-23T16:35:12+00:00

I am trying to sort records (structs) by their names. And I’m using strcmp

  • 0

I am trying to sort records (structs) by their names. And I’m using strcmp to swap to detect the alphabetical order. The swapping works fine, however it doesn’t always sort the entire list. It always leaves some records in thier incorrect alphabetic order.

void sort_by_name(){
    printf("%d\n", counter);

    int i=0, j=0;
    patient *temp;

    for(;j<=counter;j++){
        for(;i<counter-1;i++){

            if(strcmp(pRecords[i]->name, pRecords[i+1]->name) > 0){

                temp = pRecords[i];
                pRecords[i] = pRecords[i+1];
                pRecords[i+1]=temp;

            }//if loops

        }//2nd for loop

    }//1st for loop

}


counter-- number of records in the system.
  • 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-23T16:35:12+00:00Added an answer on May 23, 2026 at 4:35 pm

    I think your specific problem is that you’re not re-initialising i to 0 each time you start the inner loop.

    This is a relatively simple bubblesort where the inner loop runs over the list of N items, N times (controlled by the outer loop) to get them sorted. But your inner loop runs over them once then seems to go off elsewhere, beyond the end of the array. That’s unlikely to provide useful results 🙂

    Your more general issue is: why are you not using the language facilities to do this, specifically qsort? If you’re trying to educate yourself on sorting, that’s fine, but if you just want to sort, there’s little point re-inventing the wheel.

    For what it’s worth, I prefer the following algorithm in bubble sort. It stops early if the list is sorted and it doesn’t look at already sorted items:

    void sort_by_name(){
        int i, didSwap = 1, limit = numitems - 1;
        patient *temp;
    
        while (didSwap) {
            didSwap = 0;
            for (i = 0; i < limit; i++) {
                if (strcmp (pRecords[i]->name, pRecords[i+1]->name) > 0) {
                    temp          = pRecords[i];
                    pRecords[i]   = pRecords[i+1];
                    pRecords[i+1] = temp;
                    didSwap = 1;
                }
            }
            limit--;
        }
    }
    

    The didSwap variable controls when to exit. If you traverse the entire unsorted section without swapping then obviously, you’re finished. The limit controls that unsorted section and reduces gradually since, after each pass, there’s a greater already-sorted section at the end of the list (the higher elements bubble up to the end of the list, one per pass).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to pl/sql ! I'm trying to sort a table of records, using
I am using SortedList to arrange arraylist records dynamically in sort order by datecolumn
I am trying to sort a list of records that have been created using
I am trying to sort a list using delegates but I am getting a
I am trying to sort large inputs in the fastest way in ascending order.
I'm trying to sort a list of CLLocationDistance values by closest distance (ascending order).
I'm trying to sort a list (part of a class) in descending order containing
I asked another question: https://stackoverflow.com/questions/1180240/best-way-to-sort-1m-records-in-python where I was trying to determine the best approach
I'm trying to implement a custom sort field for a list of records. When
I'm trying to sort some records in rails and I'm passing something like created_at

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.