Sorting Strings

This is a discussion on Sorting Strings within the C++ Programming forums, part of the General Programming Boards category; Anyway, yesterday I found code for this in the source code section of this site (here). I copied that out, ...

  1. #16
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Anyway, yesterday I found code for this in the source code section of this site (here). I copied that out, modified it to C++ commands and stuff, and it generated an error on the line:
    The code from that link compiles fine for me...but can I see your modified code?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #17
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok, here it is:

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SWAP(a,b)       {int t; t=a; a=b; b=t}
    #define INDEX 8
    
    void bubble_srt(int a[], int n);
    
    int main(void)
    {
         int i;
         int array[INDEX] = {12, 8 35, 92, 19}; //Altered the numbers...
    
         cout<<"Before sorting:"<< endl;
         for(i=0; i<INDEX; i++)
         cout<< array[i]<< endl;
    
         bubble_srt(array, INDEX);
    
         cout<<"After the sort:"<< endl;
         for(i=0; i<INDEX; i++)
         cout<< array[i]<< endl;
    
         system("pause");
         return 0;
    }
    
    void bubble_srt(int a[], int n)
    {
         int i, j;
    
         for(i=0; i<n; i++)
         {
              for(j=1; j<(n-1); j++)
              {
                   if(a[j-1]<a[j])
                   SWAP(a[j-1], a[j]);
              }
         }
    }
    Sorry for indentations, I program on my laptop but its not connected to the 'net, so I can't just copy and paste.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  3. #18
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #include <iostream>
    #include <cstdlib>
    
    #define SWAP(a,b,c)       { a=c; c=b; }
    #define INDEX 8
    using namespace std;
    void bubble_srt(int a[], int n);
    
    int main(void)
    {
         
         int array[INDEX] = {12, 8, 35, 92, 19}; //Altered the numbers...
    
         cout<<"Before sorting:"<< endl;
         for(int i=0; i<INDEX; i++)
         cout<< array[i]<< endl;
    
         bubble_srt(array, INDEX);
    
         cout<<"After the sort:"<< endl;
         for(int i=0; i<INDEX; i++)
         cout<< array[i]<< endl;
    
         system("pause");
         return 0;
    }
    
    void bubble_srt(int a[], int n)
    {
         int i, j;
    
         for(i=0; i<n; i++)
         {
              for(j=1; j<(n-1); j++)
              {
                   if(a[j-1]<a[j]);
                   SWAP(a[j-1],a[j-1], a[j]);
              }
         }
    }
    You forgot a semicolon in your function and a comma in your array..also I don't really know the rules about macros since I haven't really used them, but adding another argument to use as a tmp worked...just pass the first value twice
    Code:
    SWAP(val1,val1,val2);
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #19
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok, thanks. I thought I may have just been missing something because I had to copy from one computer to another... Thanks. By the way, do you know of a way that I could make the values strings isntead of numbers?
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  5. #20
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well making the values chars would be easy enough, but strings would be a bit difficult...for chars just make the array a char array instead of an integer array
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #21
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    And when you get tired of screwin around with that C stuff, give C++ a try:

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main( int argc, char** argv ) {
    
    	string test( "da1feb2c3a" );
    
    	sort( test.begin(), test.end() );
    
    	cout <<test <<endl;
    }

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok, thanks. It still gives me an error though. It says that 'std' doesn't exist or isn't a namespace.

    Also, it said it couldn't use cstdlib.h, so I just removed that line.

    Edit: Ok, I commented out the line 'using namespace std; and it got rid of all errors and warnings. I run the program, and for 'Before sorting:' it says 8. For 'After the sort:' it displays the following:

    0
    0
    0
    0
    0
    0
    0
    0

    Now, maybe its just me, but that doesn't look like its working properly. Anybody have any idea what's going wrong?
    Last edited by Derek5272; 08-22-2003 at 10:37 PM.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  8. #23
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok, I'm gonna bump this before it slips off the page... Can't anybody help me?
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  9. #24
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Post your exact source code.

  10. #25
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok, sorry...

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define SWAP(a,b,c)       { a=c; c=b; }
    #define INDEX 8
    //using namespace std;
    void bubble_srt(int a[], int n);
    
    int main(void)
    {
         
         int array[INDEX] = {12, 8, 35, 92, 19}; //Altered the numbers...
    
         cout<<"Before sorting:"<< endl;
         for(int i=0; i<INDEX; i++)
         cout<< array[i]<< endl;
    
         bubble_srt(array, INDEX);
    
         cout<<"After the sort:"<< endl;
         for(int i=0; i<INDEX; i++)
         cout<< array[i]<< endl;
    
         system("pause");
         return 0;
    }
    
    void bubble_srt(int a[], int n)
    {
         int i, j;
    
         for(i=0; i<n; i++)
         {
              for(j=1; j<(n-1); j++)
              {
                   if(a[j-1]<a[j])
                   SWAP(a[j-1],a[j-1], a[j]);
              }
         }
    }
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  11. #26
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Your swap is wrong. Macros are, well, evil. They expand in place, so your line where you call swap evaluates to:

    Code:
    SWAP(a[j-1],a[j-1], a[j]);
           ||
           \/
    a[j] = a[j-1]; a[j-1] = a[j];
    Say a[j-1] is 33, a[j] is 4. Then, the first expression sets a[j] to 33. At this point both a[j] and a[j-1] are 33, so the next line does nothing -- that 4 is lost forever.

    Also, a swap with three parameters is confusing.

    Do this instead:

    Code:
    inline void swap(int &a, int &b){
       int temp = a;
       a = b;
       b = temp;
    }
    Your macro gains nothing by being a macro, and macros should be avoided whenever possible.

  12. #27
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Here is a more efficient way of swapping:

    Code:
    inline void swap( int* a, int* b ) {
    
    	*a ^= *b;
    	*b ^= *a;
    	*a ^= *b;
    }
    This uses the exclusive OR operation to swap the variables and does not require a temporary variable

  13. #28
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Is that really more efficent, though? It still requires three passes through an ALU, and none of the operations can happen in parallel.

  14. #29
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok, neither of those work, and I'm not even going to bother putting the errors up. Does anybody know a way to fix my code that will actually work?
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  15. #30
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Those swap methods both work just fine. The problems are in your code:

    1) You have INDEX declared as 8 but have only 5 entries.
    2) You use deprecated headers.
    3) Your bubblesort isn't actually implementing the bubblesort algorithm.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  2. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 03:29 PM
  3. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  4. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 12:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21