Solved

Bubble Sort on a Char Field

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

920 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

19 Experts available now in Live!

Get 1:1 Help Now