Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems with quickSorting a QVector
Forum Updated to NodeBB v4.3 + New Features

Problems with quickSorting a QVector

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 658 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 Offline
    M Offline
    MaKoZer
    wrote on last edited by MaKoZer
    #1

    Hello,

    i've some Problems in my Project with quickSorting a QVector.

    Its just a simple pointer QVector and i want to sort it .... strangely the sort Algorithm works in another projekt very good ...

    and SOMEHOW sometime it works, sometimes NOT .... thats so strange o0

    Error Pic: 0_1535985278711_qvecbug.jpg
    Error MSG:
    ASSERT failure in QVector<T>::operator[]: "index out of range", file e:\programme\qtcpp\5.11.1\msvc2017_64\include\qtcore\qvector.h, line 436
    16:31:09: The program has unexpectedly finished.
    16:31:09: The process was ended forcefully.
    16:31:09: [...] crashed.

    You can find the whole project here:
    https://github.com/Makozer/NpPartitionManager

    The Code: (see vectorstash.cpp )

    // Base Class
    class VectorStash {
        typedef QVector<Coin*> CoinList;
    public:
    [...]
    void quickSortDesc();
    private:
    [...]
    void quickSortDesc(quint16 leftpos, quint16 rightpos);
    CoinList coins;
    };
    
    
    // GUI just uses this simple method
    void VectorStash::quickSortDesc() {
        if (coins.size() > 1) {
            quickSortDesc(0, (coins.size() - 1));
        }
    }
    
    
    // this method is called by the simple method
    void VectorStash::quickSortDesc(quint16 leftpos, quint16 rightpos) {
        quint16 i = leftpos, j = rightpos;
        Coin* tmp;
    
        int pivot = coins[(leftpos + rightpos) / 2]->getValue();
    
        while (i <= j) {
            while (coins[i]->getValue() > pivot) { i++; }
            while (coins[j]->getValue() < pivot) { j--; }
            if (i <= j) {
                tmp = coins[i];
                coins[i] = coins[j];
                coins[j] = tmp;
                i++;
                j--;
            }
        };
        /* recursion */
        if (leftpos < j) { quickSortDesc(leftpos, j); }
        if (i < rightpos) { quickSortDesc(i, rightpos); }
    } // end quickSortDesc
    

    I cant see or find why im getting a "index out of range" error :(

    the next problem is, as a new it student im used to programm on the console and im able to print me all useful steps so i can see where it crashes, here the real big problem is that i just get this error msg and program shuts down, no way to see better information on this bug ...

    debugger wont works, too :/

    does someone sees the problem? or has a useful tip how i can do bette rresearch on this bug?

    thx =)

    greetings

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      In GUI programs you can use
      qDebug() << "Text=" << variable;
      which shows up inside qtCreator.

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Just delete your sort implementation. Why would anybody waste time implementing a custom sort?
        if coins is of type QVector<Coin*> just use std::sort(coins.begin(),coins.end(),[](Coin* a, Coin* b)->bool{return return a->getValue() < b->getValue() ;});

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        M 1 Reply Last reply
        4
        • VRoninV VRonin

          Just delete your sort implementation. Why would anybody waste time implementing a custom sort?
          if coins is of type QVector<Coin*> just use std::sort(coins.begin(),coins.end(),[](Coin* a, Coin* b)->bool{return return a->getValue() < b->getValue() ;});

          M Offline
          M Offline
          MaKoZer
          wrote on last edited by MaKoZer
          #4

          @mrjj said in Problems with quickSorting a QVector:

          Hi
          In GUI programs you can use
          qDebug() << "Text=" << variable;
          which shows up inside qtCreator.

          thanks!!! i will try it =D

          @VRonin said in Problems with quickSorting a QVector:

          Just delete your sort implementation. Why would anybody waste time implementing a custom sort?
          if coins is of type QVector<Coin*> just use std::sort(coins.begin(),coins.end(),[](Coin* a, Coin* b)->bool{return return a->getValue() < b->getValue() ;});

          well, because we had to learn the algorithm in one module and i wanted to fully understand it so i wrote it :D
          and i need it ascending and descending, depending how i choose, is that possible with std::sort? i guess so? :D
          oh wait maybe i can do it in that case ... ill have a short look

          thx for answers!

          /edit
          @VRonin
          you're pretty right, thx a lot ... everything i wanted, but without bugs =D

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved