Solved

Bubble Sort on a Char Field

Posted on 2004-10-07
1
208 Views
Last Modified: 2010-04-01
I am trying to use a bubble sort but the field I want to sort on is a Char[16]. I need to sort this based on this column char column, but as you can see I need to do some sort of comparing. Any suggestions or am I going about this all wrong?

Here is the semi bubble sort

int newpos = 3;
int j;
int k;
bool exchangeMade;
char temp[16];
k = 0;
exchangeMade = true;
        while ((k < newpos - 1) && exchangeMade)
        {      exchangeMade = false;
                ++k;
            for (j = 0; j < newpos - k; ++j)
      {
            if (memsort[j].inventory > memsort[j + 1].inventory)
                      {            
                            temp = memsort[j].inventory;
                  memsort[j].inventory = memsort[j + 1].inventory;
                  memsort[j + 1].inventory = temp;
                             exchangeMade = true;
                       }
      }
        }
0
Comment
Question by:microcoop
1 Comment
 
LVL 15

Accepted Solution

by:
efn earned 250 total points
ID: 12255497
Use library function strcmp, not >, to compare strings.  Use library function strcpy, not assignment, to copy strings.

This is assuming you are using C-style null-terminated character strings.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now