Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



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

Posts: 3
Trades: 0
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
View Public Profile
 
 
Register now for full access!
Old 11-13-2005, 06:10 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
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';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
View Public Profile Visit 0beron's homepage!
 
Old 11-14-2005, 09:17 AM
Nahele's Avatar
Extreme Talker

Posts: 204
Trades: 1
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
View Public Profile
 
Old 11-14-2005, 08:06 PM
Junior Talker

Posts: 3
Trades: 0
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, 225 views)
sweetzeze is offline
View Public Profile
 
Old 11-14-2005, 11:17 PM
Junior Talker

Posts: 3
Trades: 0
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
View Public Profile
 
Old 11-17-2005, 07:12 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.17246 seconds with 13 queries