# Bubble sort

• 02-17-2003
bearcat19
Bubble sort
I am writing a program that will rotate the letters in the vector CharList depending on the sign of the integer. I need help with the bubble sort (my RotateLetters function.) This is what I have so far.

Code:

```#include <iostream> #include <vector> #include <cmath>        //abs() using namespace std; void RotateLetters (vector <char> & CharList, int NumPositions); int main (void) {         vector <char> Alphabet (26);                 for (int  cnt = 0; cnt < 26; cnt++)                 Alphabet [cnt] = char (cnt + 'A');         cout << "How many positions do you intend to rotate this alphabet list? "                 << " '-' means rotate upward; otherwise downward." << endl;         int Positions;         cin >> Positions;         RotateLetters (Alphabet, Positions);         for (cnt = 0; cnt < Alphabet.size(); cnt++)                 cout << Alphabet[cnt];         cout << endl;         return 0; } void  RotateLetters (vector <char> & CharList, int NumPositions) {         int pairs;         int index;         for (pairs = CharList.size() - 1; pairs > 0; pairs--)                 for (index = 0; index < pairs; index++)                         if (CharList[index] > CharList[index + 1])                         {                                 int temp;                                 temp = CharList[index];                                 CharList[index] = CharList[index + 1];                                 CharList[index + 1] = temp;                         } }```
• 02-17-2003
PJYelton
You are implementing the bubble sort incorrectly. There are a couple of different ways to write it, the one that is closest to what you have is this:
Code:

```        for (pairs = 0; pairs < CharList.size(); pairs++)                 for (index = pairs+1; index < CharList.size(); index++)                         if (CharList[index] > CharList[pairs])                         {                                 int temp;                                 temp = CharList[index];                                 CharList[index] = CharList[pairs];                                 CharList[pairs] = temp;                         }```
• 02-18-2003
bearcat19
Thanks, but what do I do with *int NumPositions*?
K
• 02-18-2003
PJYelton
I am really confused by what it is you want to do, and not even sure why you need a bubble sort. Can you give an example of what you are trying to accomplish?
• 02-18-2003
bearcat19
Sure. I'm trying to rotate the letters in CharList depending on the sign of the integer. CharList is a char vector containing the 26 letters of the alphabet. NumPositions is supposed to be the desired position for the 26 letters to rotate.
Thanks,
K
• 02-18-2003
PJYelton
I'm still a little confused by what you mean by rotate. Do you mean if the user enters '1' then it prints out something like
ZABCDEFG... etc
or
BCDE......XYZA?

Either way you don't need a bubble sort since it is already sorted.
What you would need to do would be to add the value of numPositions to each letter in the vector. You would then need to check to see if the value is greater than 'Z' or less than 'A' and adjust accordingly, but I'll let you figure out how to do that ;)