Solved

Bubble Sort on a Char Field

Posted on 2004-10-07
1
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

632 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