Hi,

I'm kind of embarassed to be asking this since I should be able to figure this out by now, but I'm writing a program to sort a vector of objects alphabetically by a string member variable.

Can someone help me figure out why the bubble sort below won't work? The whole thing compiles fine but outputs the data unsorted.

for(int y = 0; y < 49; y++){
for(int x = 0; x < 49; x++){
tResult= strcmp(deansList[x].lastName.c_str(), deansList[x+1].lastName.c_str());
if(tResult > 0){
swapper = deansList[x];
deansList[x] = deansList[x+1];
deansList[x+1] = swapper;
}//if
}//loop
}//outer loop

Thanks a lot.

Dani AI

Generated

A few targeted checks that explain why the posted bubble sort can appear to do nothing (building on and ):

  • Hard-coded bounds are brittle. If deansList.size() is not exactly 50, using indices 0..49 and deansList[x+1] is either skipping elements or invoking undefined behavior. Use the container size at runtime instead of magic numbers.
  • The other common mistake is sorting a copy. If the sort routine receives the vector by value, the caller's vector remains unchanged. Make sure the routine takes std::vector<Dean>& to sort in-place, or return the sorted vector.

A concise, modern fix using the standard library (no manual strcmp) — pass the vector by reference and use std::sort with a comparator:

#include <algorithm>

void sortByLastName(std::vector<Dean>& deans) {
    std::sort(deans.begin(), deans.end(),
              [](const Dean& a, const Dean& b){ return a.lastName < b.lastName; });
}

If case-insensitive order is required, use a comparator that normalizes case before comparing. If you prefer to keep bubble sort for learning, replace the fixed 49 with size_t n = deans.size() and write your loops against n, and consider using std::vector::at or address-sanitizer to catch out-of-range accesses.

Quick debugging steps: print deansList.size() and the first few names before/after the call, add a print or breakpoint inside the sort function to confirm it is executed on the original container, and compile/run with address/sanitizers to detect UB. For reference on the standard algorithm: std::sort.

Recommended Answers

All 5 Replies

Do you have to write your own sort algorithm? If not, then just use std::sort() which is declared in <algorithm> header file

As for your program: I think the loops are wrong. And there is no need for strcmp()

int len = deansList.size();
for(int i = 0; i < (len-1); i++)
{
   for(int j = i+1; j < len; j++)
   {
       if( deansList[i].lastName > deansList[j].lastName)
       {

       }
   }
}

@Ancient Dragon - His Bubble sort looping looks fine.

That's the bubble sort I learned many many years ago. It's a more optimized version of the algorithm NervousWreck posted because it does not require as many comparisons. There are several bubble sort algorithms -- .

k, thanks for that AD. I didn't knew of this optimized version.

But NervousWreck code also looks fine to me .. don't know what's wrong in it logically.

Ok, I wrote a test program of my own using his bubble sort algorithm that he posted, and it worked ok. So there must be something else in his program that is incorrect. The problem might be that he passed the vector by value instead of by reference to that bubble sort function.

@Nervous: You will have to post the rest of the program before we can answer your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.