Reply
C++ bubble sort
Old 11-13-2005, 04:32 PM C++ bubble sort
Junior Talker

Posts: 3
Hello, i'm a new user. well i need help in making a bubble sort program that sorts words according to the alphabet. and i want it in C++.

Thanks in advance for any help.
sweetzeze is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 11-13-2005, 06:10 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
What have you got so far? Are the words in a char** or a std::vector?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 11-14-2005, 09:17 AM
Nahele's Avatar
Extreme Talker

Posts: 199
Wow...so I tried to code one up real fast and realized it has been about 6 years since I have coded C++ and don't really remember the exact syntax of everything...but I did find this:

http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm

Hopefully that helps. Maybe you should use a bogo sort instead, those are fun. Only sort that can take a completely out of order array and sort it in one pass...though there is always the chance that it will never sort correctly.
Nahele is offline
Reply With Quote
View Public Profile
 
Old 11-14-2005, 08:06 PM
Junior Talker

Posts: 3
thank u guys for trying to help, well i know how to do bubble sort for numbers using the code below, but i need to bubble sort string where words are gonna be arranged according to the alphabet. and i don't want a have a code to enter value (i don't wont cin) i want a code where i eneter the values through the parameter.


here is the code for bubble sorting numbers
Attached Files
File Type: doc bubblesort.doc (25.0 KB, 157 views)
sweetzeze is offline
Reply With Quote
View Public Profile
 
Old 11-14-2005, 11:17 PM
Junior Talker

Posts: 3
here is what i ended up with, i jthink it is almost correct, but for some reason it is giving me an error. so if anybody can figure out what is wrong i'll be very thankful.


#include <cstdlib>
#include <iostream>
using namespace std;
void alphaSort(int numStrings, char *strptr[])
{
bool finished;
do
{
finished = true;
for ( int i = 0; i < numStrings - 1; i++)
{
if ( strcmp(strptr[i], strptr[i+1]) > 0 )
{
finished = false;
char *temp = strptr[i];
strptr[i] = strptr[i+1];
strptr[i+1] = temp;
}
}
}
while (!finished);
}
int main(int argc, char *argv[])
{
alphaSort ( argc - 1, argv + 1);
for ( int i =1; i < argc; i++);
cout << argv[i] << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
sweetzeze is offline
Reply With Quote
View Public Profile
 
Old 11-17-2005, 07:12 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Code:
#include <stdio.h>
#include <string.h>

void bubble(char *string[], const int size)
{
   int pass, i;
   char *temp;

   for (pass=0; pass < size - 1; pass++) {
      for (i=0; i<size - 1; i++) {
         if (strcmp(string[i], string[i+1]) > 0) {
            temp = string[i];
            string[i] = string[i+1];
            string[i+1] = temp;
         }
      }
   }
}

int main()
{
   int i;
   char *list[] = {"orange", "pear", "apple", "aple", "Orange"};

   printf("Initial\n");

   for (i = 0; i < sizeof(*list)+1; i++) {
      printf("%d : %s\n", i, list[i]);
   }

   bubble(list, 5);
   printf("Sorted List:\n");

   for (i = 0; i < sizeof(*list)+1; i++) {
      printf("%d : %s\n", i, list[i]);
   }

   return 0;
}
Should not take you much to convert to c++

Code:
#include <string.h>
#include <iostream>

using namespace std;

void bubble(char *string[], const int size)
{
   int pass, i;
   char *temp;

   for (pass=0; pass < size - 1; pass++) {
      for (i=0; i<size - 1; i++) {
         if (strcmp(string[i], string[i+1]) > 0) {
            temp = string[i];
            string[i] = string[i+1];
            string[i+1] = temp;
         }
      }
   }
}

int main()
{
   int i;
   char *list[] = {"orange", "pear", "apple", "aple", "Orange"};

   cout << "Initial\n";

   for (i = 0; i < sizeof(*list)+1; i++) {
      cout <<  i << " " << list[i] << endl;
   }

   bubble(list, sizeof(*list));

   cout << "Sorted List:\n";

   for (i = 0; i < sizeof(*list)+1; i++) {
      cout <<  i << " " << list[i] << endl;
   }

   return 0;
}
In case you find converting C to C++ elusive.

Ibbo

Ibbo
__________________
www.nationalclubgolfer.com www.sportspub.co.uk www.bespokecc.co.uk www.centralmarquees.co.uk
Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf

Last edited by ibbo : 11-17-2005 at 07:41 AM.
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Reply     « Reply to C++ bubble sort
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13736 seconds with 13 queries