Question on Bubble sort.

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Question on Bubble sort.

Postby Dan R. » Sat Nov 22, 2003 10:35 am

assigment was to read a file of ints and sort it thats no problem. Extra credit is to read in names from a file and sort them.. Having problems making this sort strings instead of ints.. when i used strcpy and strcmp i could not get it to work.


void bubbleSort1(int x[], int n) {
for (int pass=1; pass < n; pass++) {

for (int i=0; i < n-pass; i++) {
if (x[i] > x[i+1]) {
// exchange elements
int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp;
}
}
}
}
Dan R.
 

Postby omnius » Sat Nov 22, 2003 11:09 am

You need to post the code you've been trying to use for strings for us to be able to offer concrete advice. The actual solution will depend on whether you're using C-style strings or C++ std::string class. It's not obvious to me from the code you posted whether you're coding in C or C++.

If you use the std::string class you can compare directly using < and > operators.

If using strcmp() with C-style strings (as seems likely) check the return value to see if it is less than or greater than zero.

if(strcmp(str1,str2) < 0) {
// str1 is lexographically less than str2


if(strcmp(str1,str2) > 0) {
// str1 is lexographically greater than str2
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby mfrank410 » Mon Nov 24, 2003 12:19 pm

If your sorting strings you dont want to be using strcmp(). All that function does is compare to see if two strings are equal.

You want to sort, by characters.

Sort all strings by first character.
Sort all strings by second character.
etc.
The one catch is you have to ignore the strings that are, say 3 characters in length when you are sorting the strings that are 5 characters in length.

cat
dog
mouse
bird

b
c
d
m

bi
ca
do
mo

bir
cat
dog
mou

bird
cat <-ignore
dog <-ignore
mous

bird <-ignore
cat <-ignore
dog <-ignore
mouse

If you still need some help I can post some sample code to do it.

good luck
Mike Frank
Co-Owner / Programmer
Frank & Mundula Consulting
http://www.frankmundulaconsulting.com
mfrank@frankmundulaconsulting.com
mfrank410
 
Posts: 50
Joined: Fri Oct 24, 2003 3:35 pm
Location: Ontario, Canada

Postby RecursiveS » Mon Nov 24, 2003 12:41 pm

mfrank410 wrote:If your sorting strings you dont want to be using strcmp(). All that function does is compare to see if two strings are equal.


or less than, or greater than.......

Return Value

The return value for each of these functions indicates the lexicographic relation of string1 to string2.

Value Relationship of string1 to string2

< 0 string1 less than string2
0 string1 identical to string2
> 0 string1 greater than string2

User avatar
RecursiveS
Site Admin
 
Posts: 1236
Joined: Thu Sep 18, 2003 8:33 am
Location: Dorset, UK

Postby omnius » Tue Nov 25, 2003 5:06 pm

mfrank410 wrote:If your sorting strings you dont want to be using strcmp(). All that function does is compare to see if two strings are equal.
...
...
If you still need some help I can post some sample code to do it.

Please look up the strcmp() function in your reference book before you start posting sample code. Thanks.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Postby mfrank410 » Wed Nov 26, 2003 1:31 pm

My bad...sorry your right.

Anyways, I just never use strcmp() to test strings especially sorting. I find you can write much quicker code by actually writing your own character by character sort algorithm.

But to his, his own
Mike Frank
Co-Owner / Programmer
Frank & Mundula Consulting
http://www.frankmundulaconsulting.com
mfrank@frankmundulaconsulting.com
mfrank410
 
Posts: 50
Joined: Fri Oct 24, 2003 3:35 pm
Location: Ontario, Canada

Postby RecursiveS » Wed Nov 26, 2003 5:14 pm

mfrank410 wrote:My bad...sorry your right.

Anyways, I just never use strcmp() to test strings especially sorting. I find you can write much quicker code by actually writing your own character by character sort algorithm.


omnius is just gonna love this...... :twisted: :twisted: :twisted:



And so I stray, like an innocent lamb into the slaughterhouse......

How does that work mfrank410 :?:
User avatar
RecursiveS
Site Admin
 
Posts: 1236
Joined: Thu Sep 18, 2003 8:33 am
Location: Dorset, UK

Postby RecursiveS » Wed Nov 26, 2003 5:15 pm

Not that I'm doubting you, just interested. :)
User avatar
RecursiveS
Site Admin
 
Posts: 1236
Joined: Thu Sep 18, 2003 8:33 am
Location: Dorset, UK


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests