Thread: Bubble sort

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Bubble sort

    how do i bubble sort a string array. I only want to sort a single word(no spaces) in abc order.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Search the forum - it's been discussed many times before.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    can u please direct me then. this is the last step to my program and i cant find nothing.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Anyone wanna lmk how to pass an input from a cin >> "word" into a function

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    this is what i have so far, i just need to know how to pass my cin >> "input" into my function.
    Code:
         5
         6  #include <iostream>
         7  #include <algorithm>
         8  #include <string>
         9  using namespace std;
        10
        11  void sortArray(int [], int);
        12  void showArray(int [], int);
        13
        14  int main()
        15  {
        16
        17  string name;
        18  string name1;
        19  string nameC;
        20  int i;
        21  string character;
        22
        23  // Ask user to enter a keyword
        24     cout << "Enter a keyword without spaces: ";
        25     cin >> name;
        26     nameC=name;
        27     name1=name;
        28
        29  // Displays the keyword in reverse order
        30     reverse(name.begin(),name.end());
        31       cout << name << endl;
        32
        33  // Displays keyword in Uppercase letters
        34     for (i=0; name1[i] ; i++) name1 [i]=toupper(name1[i]);
        35       cout << name1 << endl;
        36
        37  // Asks the user for what character do they want to look for
        38     size_t found;
        39     string str2;
        40       cout << "What character are we looking for? ";
        41       cin >> str2;
        42
        43  // Find the first character in the keyword
        44     found=nameC.find(str2);
        45     if (found!=string::npos)
        46       cout << "First position found at: " << int(found) << endl;
        47  
        48  // Find the last character in the keyword
        49    string str = nameC;
        50    string key = str2;
        51    found=str.rfind(key);
        52    if (found!=string::npos)
        53      cout << "Last position found at: " << int(found) << endl;
        54    else
        55      cout << "Nothing is found";
        56  
        57  // Sort the characters in order
        58    char nameC[30] = nameC;
        59    sortArray(values,6)
        60  
        61  // Display the characters in order
        62    showArray(values,6)
        63  
        64  return 0;
        65  }
        66  
        67  
        68  void sortArray(int array[],int size)
        69  {
        70    bool swap;
        71    int temp;
        72  
        73    do
        74    {
        75       swap = false;
        76       for (int count = 0; count < (size - 1); count++)
        77       {
        78          if (array[count] > array[count + 1])
        79          {
        80             temp = array[count + 1];
        81             array[count] = array[count + 1];
        82             array[count + 1] = temp;
        83             swap = true;
        84          }
        85       }
        86    } while (swap);
        87  }
        88  
        89  void showArray(int array[], int size)
        90  {
        91     for (int count = 0; count < size; count++)
        92        cout << array[count] << " ";
        93     cout << endl;
        94  }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM