Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Bubble sort in CObList [modified]

Bubble sort in CObList [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
18 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mesajflaviu

    It can be posible to (bubble) sort an ObList after some criteria ? Here I have :

    typedef CTypedPtrList<> CDrawObjList;

    and then :

    CDrawObjList ObjList;
    for(int i = 0;i < 5;++i)
    {
    CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
    // which sizeRect , and ptOrigin will be aleatory
    ObjList.AddTail(pObj)
    }

    ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :

    POSITION pos = ObjList.GetHeadPosition();
    while(pos)
    {
    	CDrawObj\* pObj1 = ObjList.GetNext(pos);
                if(pos == NULL)break;
                CDrawObj\* pObj2 = ObjList.GetNext(pos);
                if(pObj1->m\_position.left > pObj2->m\_position.left)
                    // switch pObj1 and pObj2 between them ... but how ?
    }
    

    any hint or ideas will be apreciated . Thank you !!!

    modified on Tuesday, December 14, 2010 7:38 AM

    E Offline
    E Offline
    Eugen Podsypalnikov
    wrote on last edited by
    #8

    // switch pObj1 and pObj2 between them ... but how ? Try it :) :

    void SortDrawObjects(CDrawObjList& cList)
    {
    for (int i = 0; i < cList.GetCount(); i++) {
    POSITION pos = cList.GetHeadPosition();
    while (pos) {
    POSITION posFirst = pos;
    CDrawObj* pcFirst = cList.GetNext(pos);
    if (pos) {
    POSITION posSecond = pos;
    CDrawObj* pcSecond = cList.GetNext(pos);
    if (pcFirst->m_position.left > pcSecond->m_position.left) {
    cList.SetAt(posFirst, pcSecond);
    cList.SetAt(posSecond, pcFirst);
    }
    pos = posSecond;
    }
    }
    }
    }

    They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

    M 1 Reply Last reply
    0
    • C Cedric Moonen

      Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #9

      Well, you could use the stl algorithms work with the MFC collection classes too. That's the unprecedented beauty of STL. :)

      It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

      M 1 Reply Last reply
      0
      • M mesajflaviu

        It can be posible to (bubble) sort an ObList after some criteria ? Here I have :

        typedef CTypedPtrList<> CDrawObjList;

        and then :

        CDrawObjList ObjList;
        for(int i = 0;i < 5;++i)
        {
        CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
        // which sizeRect , and ptOrigin will be aleatory
        ObjList.AddTail(pObj)
        }

        ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :

        POSITION pos = ObjList.GetHeadPosition();
        while(pos)
        {
        	CDrawObj\* pObj1 = ObjList.GetNext(pos);
                    if(pos == NULL)break;
                    CDrawObj\* pObj2 = ObjList.GetNext(pos);
                    if(pObj1->m\_position.left > pObj2->m\_position.left)
                        // switch pObj1 and pObj2 between them ... but how ?
        }
        

        any hint or ideas will be apreciated . Thank you !!!

        modified on Tuesday, December 14, 2010 7:38 AM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #10

        An algorithm after all is just a modus operandi. As long as a collection class, irrespective of the library that is part of, provides mechanism to insert and remove elements at any location, all the algorithms can be implemented. The performance may be not as expected due to the implementation of the classes themselves. What I am trying to say is, if you understand the algorithm and read the documentation for the collection class, it must not be difficult at all.

        ...byte till it megahertz... my donation to web rubbish

        1 Reply Last reply
        0
        • E Eugen Podsypalnikov

          // switch pObj1 and pObj2 between them ... but how ? Try it :) :

          void SortDrawObjects(CDrawObjList& cList)
          {
          for (int i = 0; i < cList.GetCount(); i++) {
          POSITION pos = cList.GetHeadPosition();
          while (pos) {
          POSITION posFirst = pos;
          CDrawObj* pcFirst = cList.GetNext(pos);
          if (pos) {
          POSITION posSecond = pos;
          CDrawObj* pcSecond = cList.GetNext(pos);
          if (pcFirst->m_position.left > pcSecond->m_position.left) {
          cList.SetAt(posFirst, pcSecond);
          cList.SetAt(posSecond, pcFirst);
          }
          pos = posSecond;
          }
          }
          }
          }

          They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

          M Offline
          M Offline
          mesajflaviu
          wrote on last edited by
          #11

          Well , it's work great ! Thank you very much !

          1 Reply Last reply
          0
          • R Rajesh R Subramanian

            Well, you could use the stl algorithms work with the MFC collection classes too. That's the unprecedented beauty of STL. :)

            It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

            M Offline
            M Offline
            mesajflaviu
            wrote on last edited by
            #12

            If I mix MFC with STL , I wouldn't keep consistency , or not ?

            1 Reply Last reply
            0
            • M mesajflaviu

              It can be posible to (bubble) sort an ObList after some criteria ? Here I have :

              typedef CTypedPtrList<> CDrawObjList;

              and then :

              CDrawObjList ObjList;
              for(int i = 0;i < 5;++i)
              {
              CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
              // which sizeRect , and ptOrigin will be aleatory
              ObjList.AddTail(pObj)
              }

              ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :

              POSITION pos = ObjList.GetHeadPosition();
              while(pos)
              {
              	CDrawObj\* pObj1 = ObjList.GetNext(pos);
                          if(pos == NULL)break;
                          CDrawObj\* pObj2 = ObjList.GetNext(pos);
                          if(pObj1->m\_position.left > pObj2->m\_position.left)
                              // switch pObj1 and pObj2 between them ... but how ?
              }
              

              any hint or ideas will be apreciated . Thank you !!!

              modified on Tuesday, December 14, 2010 7:38 AM

              L Offline
              L Offline
              L Braun
              wrote on last edited by
              #13

              Try google: "sort coblist" comes up with http://www.codeguru.com/cpp/cpp/cpp_mfc/collections/article.php/c757/[^] and a lot of recommendations to drop MFC :)

              M 1 Reply Last reply
              0
              • L L Braun

                Try google: "sort coblist" comes up with http://www.codeguru.com/cpp/cpp/cpp_mfc/collections/article.php/c757/[^] and a lot of recommendations to drop MFC :)

                M Offline
                M Offline
                mesajflaviu
                wrote on last edited by
                #14

                Useful code !

                1 Reply Last reply
                0
                • C Cedric Moonen

                  Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).

                  Cédric Moonen Software developer
                  Charting control [v3.0] OpenGL game tutorial in C++

                  T Offline
                  T Offline
                  thomas michaud
                  wrote on last edited by
                  #15

                  It's been a while (and I don't have my book with me) but iirc, the std::algorithm (and I think sort is in that) didn't require the structure to be a std::list. (I seem to recall standard arrays being able to be sorted with std as well.)

                  1 Reply Last reply
                  0
                  • M mesajflaviu

                    It can be posible to (bubble) sort an ObList after some criteria ? Here I have :

                    typedef CTypedPtrList<> CDrawObjList;

                    and then :

                    CDrawObjList ObjList;
                    for(int i = 0;i < 5;++i)
                    {
                    CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
                    // which sizeRect , and ptOrigin will be aleatory
                    ObjList.AddTail(pObj)
                    }

                    ok , by now seems to be 5 element in objects array My question is , how can sort elements of ObjList after follow criteria :

                    POSITION pos = ObjList.GetHeadPosition();
                    while(pos)
                    {
                    	CDrawObj\* pObj1 = ObjList.GetNext(pos);
                                if(pos == NULL)break;
                                CDrawObj\* pObj2 = ObjList.GetNext(pos);
                                if(pObj1->m\_position.left > pObj2->m\_position.left)
                                    // switch pObj1 and pObj2 between them ... but how ?
                    }
                    

                    any hint or ideas will be apreciated . Thank you !!!

                    modified on Tuesday, December 14, 2010 7:38 AM

                    M Offline
                    M Offline
                    Michael Waters
                    wrote on last edited by
                    #16

                    If you are wedded to MFC, at least use a CObArray instead of a CObList. The algorithim will be much cleaner and faster with a smaller memory footprint. For instance ...

                    typedef CTypedPtrArray<> CDrawObjArray;

                    then create the array ...

                    CDrawObjArray ObjArray;

                    const unsigned int nSize = 5;

                    ObjArray.SetSize(nSize);

                    fill the array ...

                    for(int i = 0; i < nSize; i++)
                    {
                    CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));

                    ObjArray\[i\] = pObj;
                    

                    }

                    and finally sort the array (using the most inefficient sorting algorithim known to man).

                    for(int i = 0; i < nSize; i++)
                    {
                    for(int j = i; j < nSize; j++)
                    {
                    unsigned int nPrevious = ObjArray[j-1]->m_position.left;
                    unsigned int nCurrent = ObjArray[j]->m_position.left;

                        if(nPrevious > nCurrent)
                        {
                            CDrawObj\* pObj = ObjArray\[j-1\];
                    
                            ObjArray\[j-1\] = ObjArray\[j\];
                            ObjArray\[j\] = pObj ;
                        }
                    }
                    

                    }

                    D 1 Reply Last reply
                    0
                    • M Michael Waters

                      If you are wedded to MFC, at least use a CObArray instead of a CObList. The algorithim will be much cleaner and faster with a smaller memory footprint. For instance ...

                      typedef CTypedPtrArray<> CDrawObjArray;

                      then create the array ...

                      CDrawObjArray ObjArray;

                      const unsigned int nSize = 5;

                      ObjArray.SetSize(nSize);

                      fill the array ...

                      for(int i = 0; i < nSize; i++)
                      {
                      CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));

                      ObjArray\[i\] = pObj;
                      

                      }

                      and finally sort the array (using the most inefficient sorting algorithim known to man).

                      for(int i = 0; i < nSize; i++)
                      {
                      for(int j = i; j < nSize; j++)
                      {
                      unsigned int nPrevious = ObjArray[j-1]->m_position.left;
                      unsigned int nCurrent = ObjArray[j]->m_position.left;

                          if(nPrevious > nCurrent)
                          {
                              CDrawObj\* pObj = ObjArray\[j-1\];
                      
                              ObjArray\[j-1\] = ObjArray\[j\];
                              ObjArray\[j\] = pObj ;
                          }
                      }
                      

                      }

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #17

                      Michael Waters wrote:

                      ...(using the most inefficient sorting algorithim known to man).

                      I can think of one worse.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Man who follows car will be exhausted." - Confucius

                      M 1 Reply Last reply
                      0
                      • D David Crow

                        Michael Waters wrote:

                        ...(using the most inefficient sorting algorithim known to man).

                        I can think of one worse.

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "Man who follows car will be exhausted." - Confucius

                        M Offline
                        M Offline
                        mesajflaviu
                        wrote on last edited by
                        #18

                        Guys , you're overcome me with solutions !!! I want to kindly thank you ! I was learn something here .And after all , seems to must begin use STL ... I will googling and see what I find ... You are very kind !

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups