Thread: Bubble sort

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    20

    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.
    Thanks in advance. :-)

    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;
    			}
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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;
    			}

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Thanks, but what do I do with *int NumPositions*?
    K

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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?

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    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
    Last edited by bearcat19; 02-18-2003 at 01:30 PM.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM