Thread: How do you .....

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Exclamation How do you .....

    How do you use the bubble sort in this program:

    #include <iostream>
    #include <stdlib.h>
    #include <string.h>


    int main(int argc, char *argv[])
    {
    for (int i = 0; i < argc; i++)
    cout << argv[i] << endl;
    cout << "argv[5] changed to an int is " << atoi(argv[5]);
    cout << "argv[6] changed to an int is " << atoi(argv[6]);
    for (int i = 0; i < argc-1; i++)
    if (strcmp(argv[i], argv[i+1]) > 0)
    {
    char *x = argv[i];// temporarly point to argv[i]
    argv[i] = argv[i+1];// argv[i] now points to same place as argv[i+1]
    argv[i+1] = x;// argv[i+1] now points to old argv[i]
    }
    // I think you bubble sort function here.
    // And then use the function strcmp to switch strings, but how?
    for (int i = 0; i < argc; i++)
    cout << argv[i] << endl;
    system("PAUSE");
    return 0;
    }
    // argc is a double pointer
    // *argv array of character arrays

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try this site for a bubble sort tutorial.

    Here's some sample source code for a bubble sort function.



    WARNING!!! High levels of obfuscation detected!

    Code:
    void bubbleSort(int n[],int s){int i,j;for(i=0;i<s;i++){for(j=0;j<s-1;j++){n[j]=(n[j+1]<n[j]?n[j]^=(n[j+1]^=(n[j]^=n[j+1])):n[j]);}}}
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    he he, I wonder how many folks know how to swap using ^

    gg

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Question

    So Codeplug
    How do you use ^ to swap?

    Help me out please.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read the line of code xsquared quoted. You will see how.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed