Thread: Sorting a vector numerically

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Sorting a vector numerically

    I was wondering how to sort a vector in numerical order... Is it similar to a "bubble sort" for an array... I can post the array sort code if need be...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    yes you can do a bubble sort on a vector. You can also do a number of other sorts on a vector. You can also call the sort() function in STL to sort a vector.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    As far as I know, vectors implement the sort algorithm.
    http://www.sgi.com/tech/stl/sort.html
    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.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    I need to use the bubble sort because I need to sort one vector numerically (101, 102, etc...) and then resort another vector into the proper order...
    for example:

    the values in the first vector are
    102
    103
    101
    105
    104

    and the values in the second vector are
    202.22
    122.32
    122.33
    102.00
    902.11

    when side by side:

    102 202.22
    103 122.32
    etc...

    They need to be arranged:
    101 122.33
    102 202.22
    102 122.32
    etc...

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You could try to dump the values from the two vectors into a map<int,double> container. They would then be automatically sorted as you indicated. Very easy to do.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    This is the generic bubble sort algorithm that I use... how would it be modified to use a vector?

    Code:
    void SortArray (int NumberOfItems,
                             int List [])
    {
         bool ExchangeMade;
         int Pass = 0,
              Index;
         int Number;
    
         ExchangeMade = true;
    
         while ((Pass < NumberOfItems - 1) && ExchangeMade == true)
          {
            ExchangeMade = false;
            Pass ++;
            for (Index = 0; Index < (NumberOfItems - Pass); Index++)
                  if (List [Index] > List [Index + 1])
                  {
                    Number = List [Index];
                    List [Index] = List [Index +1];
                    List [Index + 1] = Number;
                    ExchangeMade = true;
                   }
            }
    }

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    If you have a bubble sort that works with an array, than it will work with a vector. Since you have two vectors, I would assume that for every move you make with the values in the first vector you could make the same move with the values in the second vector.


    [Edit] - Ahh, you posted your bubble sort ... You can just pass in your two vectors and do the same thing. You don't need to pass the size anymore since that is saved inside the vector.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    that's all I wanted to know... I already know how to switch the values for more than one array at a time... I just wanted to make sure I could use the same algorithm or not...
    Thanks!

    BTW... how would I implement that map<int,double> thing? I probably won't do it for this project, but maybe for future reference?

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    For the map, instead of adding the data to the vector, you just add it to a map. Then it is already sorted for you (if you use the STL map).
    Code:
    #include <iostream>
    #include <map>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        srand(time(0));
        std::map<int, int> m;
        for (int i = 0; i < 20; i++)
        {
            m.insert(std::make_pair(rand() % 100, rand() % 1000));
        }
    
        std::map<int, int>::const_iterator iterPair = m.begin();
        std::map<int, int>::const_iterator iterEnd = m.end();
        for (; iterPair != iterEnd; ++iterPair)
        {
            std::cout << "FirstNum: " << iterPair->first;
            std::cout << "\tSecondNum: " << iterPair->second << std::endl;
        }
    }
    I just used rand() to come up with numbers, obviously you are getting your values from somewhere else. Since the map holds a pair of objects - key and value - you can use make_pair to insert the pair into the map. In your case, 101 is a key and 122.33 is a value. The STL map automatically sorts by key, and since your key is an int its especially easy.

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    oops...
    Last edited by criticalerror; 12-11-2003 at 07:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM