Solved

Bubble Sort on a Char Field

Posted on 2004-10-07
1
214 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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