Results 1 to 4 of 4

Thread: Help with sorting QVector< QVector<int> >

  1. #1
    Join Date
    Dec 2011
    Posts
    36
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Help with sorting QVector< QVector<int> >

    Hi,

    I have a QVector of QVectors (that is QVector< QVector <int> >. I am trying to write a basic sort algorithm for sorting the collection.
    I can't seem to get the while loop right, and I am staring myself blind on it -- so I was wondering if I can get some help.

    The outer Vector contains values 0 - 46 and the inner Vector contains positive numbers from 0 to 99.
    My goal is to swap rows so that the highest values in the inner Vector come first, but in such a way that the corresponding values (rows) of the outer Vector move along with the swap...
    Example: if vec[0][1] < vec[1][1] then swap the values in vec[0][0] and vec[0][1] with values vec[1][0] and vec[1][1] respectively.

    Here is my code:
    // I am receiving a QVector< QVector< int > > via a function. It is called vecToSort.

    QVector< int > vecSwapLow;
    QVector< int > vecSwapHigh;

    for ( int x = 1; x < vecToSort.size(); x++ ) // row-by-row of outer Vector
    {
    int iMove = x;
    int iHigh0 = vectToSort[ 1 ][ 0 ];
    int iHigh1 = vectToSort[ 1 ][ 1 ];
    int iLow0 = vectToSort[ 0 ][ 0 ];
    int iLow1 = vectToSort[ 0 ][ 1 ];
    vecSwapLow.clear()
    vecSwapLow << iLow0 << iLow1;

    while ( ( iMove > 0 ) && ( vecToSort[ iMove -1 ][ 1 ] > vecToSort[ x ][ 1 ] ) )
    {
    vecSwapHigh << iHigh0 << iHigh1;
    vecToSort.replace( ( iMove - 1 ), vecSwapHigh );
    iMove--;
    }
    vecSwapHigh.clear();
    vecToSort.replace( iMove, vecSwapLow );

    // after the repetition loop this codes puts the value that was replaced back into the outer Vector
    // problem is that if the repetition loop is not executed, it still replaces the row, so I end up with 3 or 4 sorted values and the rest until row 46 is all the same...
    }

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Help with sorting QVector< QVector<int> >

    Check if this is what you want?

    Qt Code:
    1. #include <QDebug>
    2. #include <QVector>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. const int inSize = 10;
    7. const int outSize = 10;
    8. QVector<QVector<int> > vecToSort;
    9.  
    10. // Create Vectors
    11. for(int i = 0; i < outSize; i++)
    12. {
    13. QVector<int> vect;
    14. for(int i = 0; i < inSize; i++)
    15. vect.append(qrand());
    16. vecToSort.append(vect);
    17. }
    18.  
    19. qDebug() << "Before:";
    20. for(int i = 0; i < vecToSort.size(); i++)
    21. qDebug() << vecToSort.at(i);
    22.  
    23. // Sort Vectors
    24. for(int i = 0; i < vecToSort.size(); i++)
    25. for(int j = i; j < vecToSort.size(); j++)
    26. if(vecToSort.at(i).at(0) < vecToSort.at(j).at(0))
    27. {
    28. QVector<int> tmp = vecToSort.at(i);
    29. vecToSort[i] = vecToSort.at(j);
    30. vecToSort[j] = tmp;
    31. }
    32.  
    33. qDebug() << "After:";
    34. for(int i = 0; i < vecToSort.size(); i++)
    35. qDebug() << vecToSort.at(i);
    36.  
    37. return 0;
    38. }
    39.  
    40. //Output
    41. Before:
    42. QVector(41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464)
    43. QVector(5705, 28145, 23281, 16827, 9961, 491, 2995, 11942, 4827, 5436)
    44. QVector(32391, 14604, 3902, 153, 292, 12382, 17421, 18716, 19718, 19895)
    45. QVector(5447, 21726, 14771, 11538, 1869, 19912, 25667, 26299, 17035, 9894)
    46. QVector(28703, 23811, 31322, 30333, 17673, 4664, 15141, 7711, 28253, 6868)
    47. QVector(25547, 27644, 32662, 32757, 20037, 12859, 8723, 9741, 27529, 778)
    48. QVector(12316, 3035, 22190, 1842, 288, 30106, 9040, 8942, 19264, 22648)
    49. QVector(27446, 23805, 15890, 6729, 24370, 15350, 15006, 31101, 24393, 3548)
    50. QVector(19629, 12623, 24084, 19954, 18756, 11840, 4966, 7376, 13931, 26308)
    51. QVector(16944, 32439, 24626, 11323, 5537, 21538, 16118, 2082, 22929, 16541)
    52. After:
    53. QVector(32391, 14604, 3902, 153, 292, 12382, 17421, 18716, 19718, 19895)
    54. QVector(28703, 23811, 31322, 30333, 17673, 4664, 15141, 7711, 28253, 6868)
    55. QVector(27446, 23805, 15890, 6729, 24370, 15350, 15006, 31101, 24393, 3548)
    56. QVector(25547, 27644, 32662, 32757, 20037, 12859, 8723, 9741, 27529, 778)
    57. QVector(19629, 12623, 24084, 19954, 18756, 11840, 4966, 7376, 13931, 26308)
    58. QVector(16944, 32439, 24626, 11323, 5537, 21538, 16118, 2082, 22929, 16541)
    59. QVector(12316, 3035, 22190, 1842, 288, 30106, 9040, 8942, 19264, 22648)
    60. QVector(5705, 28145, 23281, 16827, 9961, 491, 2995, 11942, 4827, 5436)
    61. QVector(5447, 21726, 14771, 11538, 1869, 19912, 25667, 26299, 17035, 9894)
    62. QVector(41, 18467, 6334, 26500, 19169, 15724, 11478, 29358, 26962, 24464)
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 10th January 2013 at 09:24.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    m3rlin (10th January 2013)

  4. #3
    Join Date
    Dec 2011
    Posts
    36
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Wink Re: Help with sorting QVector< QVector<int> >

    Hey Santosh Reddy,

    Simplicity and efficiency. Thanks. Yes indeed that was what I was looking for (except that I changed ...at(0) into at(1).
    I had read the QVector class reference, but it didn't occur to me to use the index.at for a whole row. Actually I thought it wouldn't work like that so I tried the traditional approach with a while loop, but now it makes perfectly sense.

    Thank you.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Help with sorting QVector< QVector<int> >

    Simplicity and efficiency.
    It looks simple, but it is not efficient way, copying the whole vector is costly operation (copy to temp and them copy it back). Instead a pointer based approach will be more efficient like this. (Note the changes are marked)

    Qt Code:
    1. #include <QDebug>
    2. #include <QVector>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. const int inSize = 10;
    7. const int outSize = 10;
    8. QVector<QVector<int> *> vecToSort; //<<<<
    9.  
    10. // Create Vectors
    11. for(int i = 0; i < outSize; i++)
    12. {
    13. QVector<int> *vect = new QVector<int>(); //<<<<
    14. for(int i = 0; i < inSize; i++)
    15. vect->append(qrand()); //<<<<
    16. vecToSort.append(vect);
    17. }
    18.  
    19. qDebug() << "Before:";
    20. for(int i = 0; i < vecToSort.size(); i++)
    21. qDebug() << *vecToSort.at(i); //<<<<
    22.  
    23. // Sort Vectors
    24. for(int i = 0; i < vecToSort.size(); i++)
    25. for(int j = i; j < vecToSort.size(); j++)
    26. if(vecToSort.at(i)->at(0) < vecToSort.at(j)->at(0)) //<<<<
    27. {
    28. QVector<int> *tmp = vecToSort.at(i); //<<<<
    29. vecToSort[i] = vecToSort.at(j);
    30. vecToSort[j] = tmp;
    31. }
    32.  
    33. qDebug() << "After:";
    34. for(int i = 0; i < vecToSort.size(); i++)
    35. qDebug() << *vecToSort.at(i); //<<<<
    36.  
    37. return 0;
    38. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. sort a Qvector based on another Qvector
    By OzQTNoob in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2012, 07:39
  2. Replies: 5
    Last Post: 3rd September 2011, 00:11
  3. Qvector
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2007, 11:46
  4. QVector
    By sabeesh in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2007, 15:37
  5. Sorting a QVector
    By jiveaxe in forum Qt Programming
    Replies: 21
    Last Post: 11th August 2007, 20:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.